<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var max1 = 100000; // 100,000 (100 Thousand)
var max2 = 10000000; // 10,000,000 (10 Million)
var max3 = 100000000; // 100,000,000 (100 Million)
var arr1 = [];
//for (var i = 0; i <= max1; i++) { arr1.push(i); }
var arr2 = [];
for (var i = 0; i <= max2; i++) { arr2.push(i); }
var arr3 = [];
//for (var i = 0; i <= max3; i++) { arr3.push(i); }
arr2.filter(function (element) {
return element === 900;
});
_.filter(arr2, function (element) {
return element === 900;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native | |
Lodash.js filter |
Test name | Executions per second |
---|---|
Native | 3.8 Ops/sec |
Lodash.js filter | 3.6 Ops/sec |
I'll break down the provided JSON benchmark definition and test cases to explain what's being tested, compared, and its pros/cons.
Benchmark Definition
The benchmark defines two tests: one native (JavaScript) and another using Lodash.js library. Both tests use the filter()
method on an array of numbers generated by pushing values from 0 to a large number (100k, 10M, or 100M).
Native Filter Test
arr1
, arr2
, arr3
) and pushes numbers from 0 to max1
, max2
, and max3
respectively.filter()
method on each array to find a specific element (900) using an anonymous function as the callback.Lodash.js Filter Test
arr2
) and pushes numbers from 0 to max2
and max3
._filter()
function from Lodash.js on each array, passing in arr2
or arr3
, and an anonymous function as the callback to find a specific element (900).Comparison
The two tests are compared to determine which one is faster. The results show that the native JavaScript filter()
method outperforms Lodash.js _filter()
function.
Pros/Cons of Different Approaches
Native JavaScript filter()
:
filter()
method implementation.Lodash.js _filter()
:
Other Considerations
filter()
method differently, affecting the results._filter()
for better performance.Alternatives
Other alternatives to native JavaScript filter()
or Lodash.js _filter()
include:
filter()
methodArray.prototype.filter()
from other libraries (e.g., jQuery, Moment.js)