<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
function isEven(n) {
return !(n%2);
}
var data = [Array(20)].map((v, idx) => idx);
_.map(isEven, data);
data.map(isEven);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash fp | |
es6 map |
Test name | Executions per second |
---|---|
lodash fp | 5263494.0 Ops/sec |
es6 map | 15681969.0 Ops/sec |
The benchmark defined in the JSON compares the performance of two approaches for mapping over an array of numbers to determine which numbers are even: using Lodash’s map
function versus the native ES6 map
method. The code utilizes the function isEven(n)
, which checks whether a number n
is even. The array being tested (data
) consists of the numbers from 0 to 19, created using ES6 syntax.
Lodash FP _.map(isEven, data)
ES6 Native data.map(isEven)
map
method of arrays in ES6. It creates a new array populated with the results of calling a provided function (isEven
) on every element in the calling array._.map
map
map
method is optimized by JavaScript engines and is generally faster for basic mapping tasks due to minimized overhead.The results show that the native es6 map
outperformed the lodash fp
version significantly:
es6 map
: 15,681,969lodash fp
: 5,263,494When deciding between these approaches, developers should assess their requirements regarding performance, code readability, and dependency management. While Lodash can enhance functional programming paradigms and make code potentially easier to read for those familiar with its syntax, its performance overhead for simple tasks like mapping may not be justifiable in many circumstances.
Aside from Lodash and ES6’s map
, other alternatives include:
Ultimately, the choice of which approach to utilize will depend on the specific context of the project and the performance requirements of the application.