<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = [{key:0,value:'majo'},{key:1,value:'aaaads'},{key:2,value:'cdsdfs'}];
_.forEach(arr,item=>{});
arr.forEach(element => {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
native |
Test name | Executions per second |
---|---|
lodash | 552720.2 Ops/sec |
native | 2497463.8 Ops/sec |
I'll explain the benchmark in detail, covering what's being tested, the options compared, pros and cons of each approach, and other considerations.
What is being tested?
The benchmark measures the performance difference between two approaches: using the built-in forEach
method (native) versus a library function from Lodash (lodash
). The test case creates an array arr
with three elements and applies a simple callback function to each element. The objective is to determine which approach is faster.
Options compared
There are two options being compared:
forEach
: This method is built into the JavaScript language and is executed by the engine (V8 in this case). It's a straightforward way to iterate over an array._lodash.forEach
: Lodash is a popular utility library for JavaScript that provides a wide range of functions for common tasks, including iteration. The forEach
function from Lodash is implemented using the native forEach
method under the hood but adds additional features like argument validation and support for iteratees (functions).Pros and Cons
forEach
:forEach
signature._lodash.forEach
:Library: Lodash
Lodash is a utility library developed by Isaac Zalcik in 2012. It provides over 160 functions for common tasks like array manipulation, object transformation, and event handling. The forEach
function from Lodash is implemented using the native forEach
method to ensure compatibility across different browsers and environments.
Special JS feature or syntax
There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. The test case only uses standard JavaScript syntax for creating arrays, defining functions, and iterating over them.
Alternatives
Other alternatives for iterating over arrays include:
for
loop: A traditional loop construct that's often used instead of forEach
.Array.prototype.forEach.call()
: This method is similar to the native forEach
method but can be used with any array-like object._map
and _reduce
functions.Keep in mind that these alternatives may have different performance characteristics or use cases compared to the native forEach
method or Lodash's forEach
function.