<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
var value = [{a: 30310}, {b: 100303}, {c: 3040494}, {d: 6542321}, {e: 13123531}]
_.each(value, function(v,i) {return v})
_.map(value, function(v,i) {return v})
value.forEach(function(v,i) {return v})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash each | |
lodash map | |
native forEach |
Test name | Executions per second |
---|---|
lodash each | 3117085.5 Ops/sec |
lodash map | 2961371.0 Ops/sec |
native forEach | 14329053.0 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
The test consists of three individual microbenchmarks that measure the performance of different JavaScript iteration methods:
_.each(value, function(v,i) {return v})
: This benchmark uses the Lodash library to iterate over an array using the each
method._.map(value, function(v,i) {return v})
: This benchmark also uses the Lodash library to iterate over an array using the map
method.value.forEach(function(v,i) {return v})
: This benchmark tests the native forEach
method of JavaScript arrays.Lodash Libraries
In this benchmark, two libraries are used:
lodash
(version 4.17.10): A popular utility library that provides a set of functional programming helpers.each
and map
methods are used to iterate over the value
array.Native JavaScript Methods
The native forEach
method is tested as well, which is part of the ECMAScript standard and does not require any libraries or additional setup.
Benchmark Options Compared
In this benchmark, three options are compared:
each
methodmap
methodforEach
methodEach option has its pros and cons:
map
) or additional memory allocations. Additionally, since these functions are part of the Lodash library, users need to include the library in their codebase.each
and map
, especially for developers unfamiliar with these methods.Other Considerations
return v
) over an array of values. This is a simplified test case that does not cover all possible scenarios, so results should be interpreted with caution.Alternatives
If you're interested in exploring alternative iteration methods for your own use cases, consider the following:
Keep in mind that the choice of iteration method depends on your specific use case, performance requirements, and personal preference.