<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
var values = Array(10000).fill(null).map((x, a) => ({number: a}));
var filterFn = x => !(x.number % 5);
var mapFn = x => x.number;
var reduceFn = (a, b) => a > b ? a : b;
const result1 = values.filter(filterFn).map(mapFn).reduce(reduceFn);
console.log(result1);
const result2 = _(values).filter(filterFn).map(mapFn).max();
console.log(result2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native array functions | |
Lodash |
Test name | Executions per second |
---|---|
Native array functions | 37549.9 Ops/sec |
Lodash | 36757.1 Ops/sec |
Let's break down the benchmark and explain what is being tested.
Benchmark Definition
The benchmark is comparing two approaches:
values
array.values
array.Options Compared
The options being compared are:
filterFn
) to exclude elements from the filtered array (native) vs using the filter()
method provided by Lodash (_(values).filter(filterFn)
).mapFn
) (native) vs using the map()
method provided by Lodash (_(values).map(mapFn)
).reduceFn
) (native) vs using the max()
method provided by Lodash (_(values).max()
).Pros and Cons of Each Approach
Native Array Functions
Pros:
Cons:
Lodash
Pros:
Cons:
Other Considerations
memoize()
function that can help with this.Library: Lodash
Lodash is a popular JavaScript utility library that provides a collection of functions for common tasks like filtering, mapping, reducing, and more. The filter()
, map()
, and max()
methods mentioned in the benchmark are part of Lodash's API. By using these methods, developers can simplify their code and focus on the logic of their application rather than rewriting manual implementations.
Special JS Feature or Syntax
There is no special JavaScript feature or syntax used in this benchmark. However, note that some modern browsers support features like async/await
and class
for handling promises, which could be relevant when writing tests for other benchmarks.
Other Alternatives
If you need to compare performance of native array methods against Lodash's implementation, here are a few alternatives:
: Using the
forEach()method instead of
filter(),
map(), or
reduce()` for the operations being compared.