//Create an array of 1000 random intergers between 1 and 10000
var arrRandom = [];
for(var intCtr=0; intCtr<1000; intCtr++) {
arrRandom.push(Math.floor(Math.random() * Math.floor(10000)));
}
function reduceCallback(accum, curr) {
return accum+curr;
}
function doRedeuce(pArray) {
return pArray.reduce(reduceCallback);
}
function doForLoop(pArray) {
var accum = 0;
var arrLength = pArray.length;
for(var intCtr=0; intCtr<arrLength; intCtr++) {
accum += pArray[intCtr];
}
return accum;
}
function doForOfLoop(pArray) {
var accum = 0;
for(var value of pArray) {
accum += value;
}
return accum;
}
function doForEach(pArray) {
var accum = 0;
pArray.forEach(function(item) {
accum += item;
});
}
var redeuceResult=0;
redeuceResult = doRedeuce(arrRandom);
var loopResult=0;
loopResult = doForLoop(arrRandom);
var forEachResult=0
forEachResult = doForEach(arrRandom)
var forOfResult=0
forOfResult = doForOfLoop(arrRandom)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce | |
for loop | |
forEach | |
for of loop |
Test name | Executions per second |
---|---|
reduce | 44698.5 Ops/sec |
for loop | 656834.1 Ops/sec |
forEach | 384501.5 Ops/sec |
for of loop | 557607.2 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Overview
The benchmark is designed to compare the performance of four different approaches for summing up an array of random numbers:
Array.prototype.reduce()
for
loopArray.prototype.forEach()
(note: this method does not return the accumulated value, but we're using it as if it did)for...of
loopOptions Compared
The benchmark compares the performance of each option by executing them multiple times and measuring their execution time per second.
reduce()
: uses an arrow function to specify the callback function for the reductionfor
loop: uses a traditional for
loop with a counter variableforEach()
(as if it returned the accumulated value): uses the forEach()
method without modifying it, as if it were returning the accumulated valuefor...of
loop: uses a for...of
loop to iterate over the arrayPros and Cons of Each Approach
reduce()
:for
loop:reduce()
or forEach()
forEach()
(as if it returned the accumulated value):reduce()
in syntax and intent, avoids explicit loopsArray.prototype.forEach()
, might be less efficient due to additional overheadfor...of
loop:Library Usage
In this benchmark, the reduce()
method uses an arrow function to specify the callback function for the reduction.
Special JS Feature/Syntax
The benchmark does not use any special JavaScript features or syntax that are not widely supported (e.g., ES6+ syntax). However, it's worth noting that the for...of
loop is a relatively new feature that was introduced in ECMAScript 2015.
Alternative Approaches
Other alternatives for summing up an array of numbers could include:
Array.prototype.map()
and then calling reduce()
on the resulting mapped arraywhile
loop or other iterative approachThese alternative approaches might have different performance characteristics, syntax requirements, and ease of use compared to the options tested in this benchmark.