<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
var params = [ "hello", true, 7 ];
var index = 0;
params.forEach(param => index++);
var params = [ "hello", true, 7 ];
var index = 0;
_.each(params, param => index++);
var params = [ "hello", true, 7 ];
var index = 0;
$.each(params, param => index++);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.forEach | |
lodash each | |
Jquery each |
Test name | Executions per second |
---|---|
Array.prototype.forEach | 86361056.0 Ops/sec |
lodash each | 3488025.5 Ops/sec |
Jquery each | 2829904.2 Ops/sec |
Overview
The provided benchmark definition and test cases are designed to compare the performance of three different ways to iterate over an array in JavaScript: Array.prototype.forEach
, Lodash's _.each
, and jQuery's $
. Each test case uses a small array with three elements and increments a variable for each iteration.
What is being tested
The benchmark is testing the execution speed of these three methods:
Array.prototype.forEach
: A built-in JavaScript method that iterates over an array, executing a provided callback function for each element._.each
: A utility function from the Lodash library that provides a functional way to iterate over arrays and objects.$
.each`: A method from the jQuery library that allows iterating over arrays using a callback function.Options compared
The options being compared are:
Array.prototype.forEach
_.each
$
.eachPros and Cons of each approach:
Array.prototype.forEach
:_.each
:$
.each`:Library usage
In the benchmark code, Lodash's _.each
is used to iterate over the array. The $.each
method from jQuery is also used, but it requires including the jQuery library in the HTML header.
Special JS feature or syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on comparing the performance of three different iteration methods.
Other alternatives
If not using Array.prototype.forEach
, other alternatives for iterating over arrays could include:
for
loopsforEach
-like methods from other libraries (e.g., Ramda)However, these alternatives are not included in the benchmark.