<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var data = Array(1000000).fill({ filtering: true, mapping: 42 });
data.filter(({ filtering }) => filtering).map(({ mapping }) => mapping)
_.map(_.filter(data, 'filtering'), 'mapping')
_.reduce(data, function(result, value, key) {
if (value.filtering) {
result.push(value.mapping)
}
return result;
}, [])
data.reduce(function(result, value, key) {
if (value.filtering) {
result.push(value.mapping)
}
return result;
}, [])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native lodash filter map | |
Lazy Lodash filter-map | |
lodash reduce | |
native reduce |
Test name | Executions per second |
---|---|
native lodash filter map | 39.9 Ops/sec |
Lazy Lodash filter-map | 31.6 Ops/sec |
lodash reduce | 69.0 Ops/sec |
native reduce | 76.2 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares different ways to perform a common data processing task: filtering an array of objects based on a specific property, and then mapping each object to extract a specific value from it. The test cases use either native JavaScript methods or the Lodash library to achieve this goal.
Script Preparation Code
The script preparation code creates an array data
with 1 million objects, each containing two properties: filtering
(a boolean) and mapping
(an integer). This data will be used as input for the test cases.
Html Preparation Code
The HTML preparation code includes a reference to the Lodash library (lodash.min.js
) from a CDN. This library is used in some of the test cases.
Test Cases
There are four test cases:
Array.prototype.filter()
and Array.prototype.map()
to achieve the filtering and mapping operations._.filter()
and _.map()
functions from the Lodash library to perform the same filtering and mapping operations._.reduce()
function from the Lodash library to achieve a similar result, although with a different approach.Array.prototype.reduce()
to achieve the filtering and mapping operations.Options Compared
The benchmark compares four options:
Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
Library Used
The Lodash library is used in two test cases:
Lodash provides a set of utility functions that make common data processing tasks easier and more efficient. However, it also adds overhead due to the external dependencies and the added abstraction.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark, other than the use of _.
notation for Lodash functions.
Other Alternatives
If you're looking for alternative ways to perform similar data processing tasks, consider the following options:
Array.prototype.flatMap()
, Promise.all()
, etc can also be used to achieve similar results.In conclusion, this benchmark helps evaluate the performance of different approaches to filtering and mapping data in JavaScript. By comparing native JavaScript methods with Lodash library functions, developers can choose the most efficient approach for their specific use case.