<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var obj = Array(1000)
.fill()
.reduce((acc, curr, i) => {
acc[i] = i;
return acc;
}, []);
var predicate = v => v % 2 === 0;
_.pickBy(obj, predicate);
obj.filter(e => predicate(e))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.pickBy | |
filter |
Test name | Executions per second |
---|---|
_.pickBy | 15570.8 Ops/sec |
filter | 673917.4 Ops/sec |
Let's break down the provided JSON data and explain what's being tested.
Benchmark Definition
The benchmark measures the performance difference between two JavaScript functions: _.pickBy
from Lodash and filter
(built-in array method).
Script Preparation Code
The script creates an array of 1000 elements, where each element is assigned a unique index using the reduce
method. This array will be used as the input data for both benchmark tests.
var obj = Array(1000)
.fill()
.reduce((acc, curr, i) => {
acc[i] = i;
return acc;
}, []);
Html Preparation Code
The HTML code includes a script tag that loads the Lodash library version 4.17.5.
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Benchmark Test Cases
There are two test cases:
_.pickBy(obj, predicate)
obj.filter(e => predicate(e))
These test cases compare the performance of the _.pickBy
function from Lodash and the built-in filter
method.
Options Compared
The benchmark tests the following options:
_._pickBy
: A function from the Lodash library that creates a new array with only the elements for which the predicate returns true.filter
: A built-in array method that creates a new array with only the elements for which the callback function returns true.Pros and Cons of Each Approach
Library: Lodash
Lodash is a popular JavaScript utility library that provides various functions to simplify common programming tasks. In this case, the _pickBy
function creates a new array with filtered elements, making it an efficient and concise solution for filtering arrays.
Special JS Feature/Syntax
There are no special features or syntaxes used in these benchmark tests. They rely on standard JavaScript syntax and built-in methods.
Alternatives
Other alternatives to _.pickBy
include:
Array.prototype.filter()
method._pickBy
function with various options for filtering arrays.In summary, the benchmark measures the performance difference between the Lodash _pickBy
function and the built-in filter
method in JavaScript. It tests these two approaches on an array of 1000 elements to determine which one is faster and more efficient.