<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = Array.from(Array(100000).keys())
_.filter(arr, o => o !== 999999);
_.without(arr, 999999);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
filter | |
without |
Test name | Executions per second |
---|---|
filter | 1006.9 Ops/sec |
without | 1579.1 Ops/sec |
Let's break down the benchmark and explain what is being tested.
Benchmark Definition
The provided JSON defines two microbenchmarks: Lodash.filter
and Lodash.without
. The benchmarks are comparing the performance of these two Lodash functions, which are part of a popular JavaScript utility library called Lodash.
What is Lodash?
Lodash is a comprehensive JavaScript library that provides a lot of useful functions for tasks such as data manipulation, object creation, string manipulation, and more. It was created by Jeremy Ashkenas in 2011 and has since become one of the most popular JavaScript libraries.
The two benchmarked functions are:
_.filter(arr, o => o !== 999999)
: This function takes an array (arr
) as input and returns a new array containing only the elements that do not match the specified condition (o !== 999999
)._.without(arr, 999999)
: This function takes an array (arr
) as input and returns a new array containing all elements except those specified in the second argument (999999
).Options Compared
The benchmark is comparing two options:
filter
method.Pros and Cons of Each Approach
Special Considerations
The benchmark is using a special JavaScript feature called "Arrow functions" (. => o !== 999999
), which was introduced in ECMAScript 2015 (ES6). This allows for concise and readable code by eliminating the need to declare a function with function() { ... }
.
Other Alternatives
If you want to implement the logic from scratch without using Lodash, you can use a simple loop or recursion to achieve the same result. Here is an example of how you could write it:
arr = arr.filter(x => x !== 999999);
Or, using recursion:
arr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== 999999) {
arr.push(arr[i]);
}
}
Note that these implementations may not be as efficient as Lodash's optimized implementation.