<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = [
{ 'name': 'lim', 'age': 26 },
{ 'name': 'kim', 'age': 28 },
{ 'name': 'choi', 'age': 32 },
{ 'name': 'park', 'age': 21 }
];
_.forEach(array, (arr, index, self) => {
console.log(arr, index);
});
_.each(array, (arr, index, self) => {
console.log(arr, index);
});
array.forEach((arr, index, self) => {
console.log(arr, index);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash forEach method | |
lodash each method | |
es6 forEach method |
Test name | Executions per second |
---|---|
lodash forEach method | 24811.0 Ops/sec |
lodash each method | 25328.5 Ops/sec |
es6 forEach method | 28208.1 Ops/sec |
Overview
The provided JSON represents a JavaScript benchmark test case, specifically comparing the performance of lodash
's forEach
and each
methods against the built-in forEach
method in modern JavaScript (ES6).
Options Compared
Two main options are being compared:
lodash.forEach(array, (arr, index, self) => { console.log(arr, index); });
: This is using the forEach
method from the popular JavaScript utility library lodash
.array.forEach((arr, index, self) => { console.log(arr, index); });
: This is using the built-in forEach
method in modern JavaScript (ES6).Pros and Cons
lodash.forEach
:lodash
) that may not be included in the user's project setup.forEach
(ES6):Library: Lodash
Lodash
is a popular JavaScript utility library that provides a collection of functions for various tasks, such as array manipulation, string processing, and object transformation. The forEach
method in lodash
is one of its most widely used functions, offering a simple way to iterate over arrays or other iterable objects.
Special JS Feature/Syntax
The test case uses the modern JavaScript syntax (ES6) for the built-in forEach
method. This syntax was introduced in ECMAScript 2015 and provides a concise way to iterate over arrays using an arrow function.
Alternative Options
If you want to compare other options, some alternatives could be:
for...of
loop instead of forEach
: This would eliminate the need for the extra dependency (lodash
) but might not be as readable or maintainable.lodash
, you could create a benchmark test case comparing its forEach
method against the built-in ES6 method.Keep in mind that each alternative option would require modifying the benchmark test case and might not provide a direct comparison with lodash
's forEach
method.