<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
const foo = ['field1', 'field2', 'field3', 'field4', 'field5'];
_.reject(foo, nextField => nextField === 'field4');
const foo = ['field1', 'field2', 'field3', 'field4', 'field5'];
_.filter(foo, nextField => nextField !== 'field4');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
omit | |
filter |
Test name | Executions per second |
---|---|
omit | 4880638.5 Ops/sec |
filter | 5173299.0 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance of two different approaches to filter out an item from an array: _.reject
and _.filter
. Both functions are part of the Lodash library, which is a JavaScript utility library.
In this benchmark, two test cases are defined:
_reject
: The function takes an array as input and a callback function that returns a boolean value indicating whether to include or exclude the item from the array.filter
: A similar concept, but instead of excluding items, it includes only the items for which the callback function returns true
.Options compared
The two options being compared are:
_reject
: Excludes an item from the array if the callback function returns false
.filter
: Includes an item in the array only if the callback function returns true
.Pros and Cons of each approach:
_reject
:filter
:_reject
for very large arrays, since it needs to iterate over all items before returning.Lodash library
The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks such as:
_.filter
, _.map
, _.reduce
)_.pick
, _.omit
, _.clone
)_.truncate
, _.escape
)_.round
, _.ceil
)The Lodash library is widely used in JavaScript applications, including web development, mobile app development, and server-side programming.
No special JS feature or syntax
There are no special JavaScript features or syntax mentioned in this benchmark. The code uses standard JavaScript syntax and functions.
Other alternatives
If you don't want to use Lodash, there are other libraries and built-in functions that can perform similar operations:
Array.prototype.filter()
method: This is the most straightforward way to filter an array in modern JavaScript.Array.prototype.indexOf()
method followed by slicing: This can be used to filter an array, but it's less efficient than using a library or built-in function like filter()
.In summary, the benchmark is testing the performance of two approaches: _reject
and filter
. The choice between these approaches depends on your specific use case and requirements.