<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
let maxNum = 10000000; // 10,000,000 (10 Million)
var arr = [];
for (let i = 0; i <= maxNum; i++) { arr.push(i); }
arr.filter((i) => i % 2 === 0);
_.filter(arr,(i) => i % 2 === 0);
arr.map((i) => i + 1 );
_.map(arr,(i) => i + 1 );
arr.reduce((sum, i) => sum + i, 0)
_.reduce(arr, (sum, i) => sum + i, 0)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native filter | |
Lodash.js filter | |
Native map | |
Lodash.js map | |
Native reduce | |
Lodash.js reduse |
Test name | Executions per second |
---|---|
Native filter | 3.7 Ops/sec |
Lodash.js filter | 3.7 Ops/sec |
Native map | 5.2 Ops/sec |
Lodash.js map | 5.5 Ops/sec |
Native reduce | 5.8 Ops/sec |
Lodash.js reduse | 6.0 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Definition
The benchmark compares the performance of two approaches:
filter
, map
, reduce
) without any additional libraries or modifications.Options Compared
The benchmark compares the performance of:
filter
and _.filter
(Lodash.js equivalent)map
and _.map
(Lodash.js equivalent)reduce
and _.reduce
(Lodash.js equivalent)Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Library (Lodash)
Lodash is a popular utility library that provides various functions and helpers for tasks like:
filter
, map
, reduce
)upperCase
, lowerCase
)clone
, merge
)curry
, bindall
)Lodash aims to provide a small, modular set of functions that can be easily combined to achieve complex tasks.
Special JavaScript Feature
There is no special JavaScript feature or syntax mentioned in the benchmark. However, it's worth noting that some modern browsers have optimized implementations of array methods, such as filter
and map
, which can impact performance.
Other Alternatives
If you're looking for alternative libraries to Lodash, consider:
In summary, the benchmark compares the performance of native JavaScript implementations versus Lodash.js for common array operations. While native implementations may have some overhead due to library loading, Lodash.js provides optimized implementations with additional functionality and edge case handling.