<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
window.foo = [{}, {a:1}, {b:1}];
_.filter(foo, (item) => {
return !_.isEmpty(item);
});
foo.filter((item) => {
return Object.keys(item).length !== 0;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Filter | |
Native Filter |
Test name | Executions per second |
---|---|
Lodash Filter | 1279761.1 Ops/sec |
Native Filter | 1637315.6 Ops/sec |
Let's break down the provided JSON data and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches to filter an array:
_.filter
function from the Lodash library is used, which takes an array (foo
) as input and a callback function that returns a boolean value indicating whether each element should be included in the filtered array. In this case, the callback function checks if the item is not empty using the _isEmpty
function.filter
method of the native JavaScript Array prototype is used, which also takes an array as input and a callback function that returns a boolean value indicating whether each element should be included in the filtered array. In this case, the callback function checks if the item has at least one key using Object.keys(item).length !== 0
.Options Compared
The two options being compared are:
_filter
method from the Lodash library)filter
method of the native JavaScript Array prototype)Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Lodash Filter
Pros:
.map
, .filter
, and .reduce
.Cons:
Native Filter
Pros:
Cons:
filter
method's behavior and callback function syntax.Library Used
The Lodash library is used in this benchmark. The _isEmpty
function from Lodash is called to check if an object is empty, which is then used in the filter callback function.
Special JS Feature/Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. Both approaches use standard JavaScript array methods and callbacks.
Other Alternatives
If you're interested in exploring alternative filtering libraries, some popular options include:
filter
method can be used for simple filtering.Keep in mind that these alternatives may have different performance characteristics and usage patterns compared to Lodash or native implementations.