var arr = [];
for (var i = 0; i < 12345; i++) {
arr[i] = i;
}
function someFn(i) {
return (i * 3 * 8 / 1200 * 0.002 / 40 * 0.2);
}
var sumForEach = 0,
sumReduce = 0,
sumMap = 0,
sumFilter = 0,
sumFor = 0,
sumWhile = 0;
arr.forEach(item => sumForEach += someFn(item));
sumReduce = arr.reduce((lastValue, item) => {
return sumReduce += someFn(item);
});
arr.map(item => (sumMap += someFn(item)));
arr.filter(item => (sumFilter += someFn(item)));
for (var j = 0; j < arr.length; j++) {
sumFor += arr[j];
}
var l = arr.length;
var i = 0;
while (i < l) {
sumWhile += arr[i];
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forEach | |
reduce | |
map | |
filter | |
for | |
while |
Test name | Executions per second |
---|---|
forEach | 347.7 Ops/sec |
reduce | 348.9 Ops/sec |
map | 336.4 Ops/sec |
filter | 339.4 Ops/sec |
for | 298.6 Ops/sec |
while | 381.9 Ops/sec |
Let's break down what is being tested in this benchmark.
The main task of the benchmark is to measure the performance of different iteration techniques in JavaScript: forEach
, reduce
, map
, filter
, for
, and while
. The test creates an array of 12345 elements, initializes a variable sum
to store the sum, and defines a function someFn
that calculates a value based on the input.
The test cases compare the performance of each iteration technique by executing the same code with the same initial conditions. The results are measured in terms of executions per second.
Now, let's analyze the options being compared:
forEach
: This method iterates over an array using the for...of
loop (not explicitly used in this benchmark) and calls a provided function for each element.reduce
: This method iterates over an array using a reduction function that is applied to each element.map
: This method creates a new array by applying a transformation function to each element.filter
: This method creates a new array by filtering elements based on a provided function.for
: This method uses an explicit loop to iterate over an array using an index variable.while
: This method uses a conditional loop to iterate over an array using an index variable.Other considerations:
someFn
function is not optimized for performance, which may impact the overall benchmark results. A more efficient implementation could potentially improve the performance of each iteration technique.As for libraries or frameworks, there are no notable ones mentioned in this benchmark definition. However, some modern JavaScript frameworks like React or Angular might use these methods internally and provide additional optimizations.
Some special JS features or syntax used in this benchmark include:
for...of
loop is not explicitly used, but the forEach
method uses a similar syntax to iterate over arrays.reduce
, map
, and filter
methods are part of the ECMAScript 2015 (ES6) standard.Other alternatives for these iteration techniques include:
for...in
or for...of
loops with array indices for iterating over arrays.Note that this benchmark is designed to test the performance of different iteration techniques in JavaScript. The specific results may vary depending on the browser, hardware, and other factors.