Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Array.reduce vs for loop vs Array.forEach vs decrement while 2
A test summing 1000 random numbers, 1 - 10000
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/120.0
Browser:
Firefox 120
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
reduce
7177.8 Ops/sec
for loop
0.0 Ops/sec
forEach
9286.2 Ops/sec
decrement while
9928.7 Ops/sec
Script Preparation code:
// 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, index) { accum[curr] = index; return accum; } function doRedeuce(pArray) { return pArray.reduce(reduceCallback, {}); } function doLoop(pArray) { var accum = {}; for(var intCtr=0; intCtr<pArray.length; intCtr++) { accum[pArray[index]] = index; } return accum; } function decrWhile(pArray) { var accum = {}; var index = pArray.length; while(index--) { accum[pArray[index]] = index; } return accum; } function doForEach(pArray) { var accum = {}; pArray.forEach(function(item, index) { accum[item] = index; }); return accum; }
Tests:
reduce
var redeuceResult=0; redeuceResult = doRedeuce(arrRandom);
for loop
var loopResult=0; loopResult = doLoop(arrRandom);
forEach
var forEachResult=0; forEachResult = doForEach(arrRandom);
decrement while
var decrWhileResult=0; decrWhileResult = decrWhile(arrRandom);