Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
Test which looping method has the best performance regarding getting the total sum of an array of integers in Javascript
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 16_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1
Browser:
Chrome Mobile iOS 120
Operating system:
iOS 16.1
Device Platform:
Mobile
Date tested:
2 years ago
Test name
Executions per second
for of
67026.3 Ops/sec
forEach
40385.9 Ops/sec
reduce
50381.9 Ops/sec
for i
1720.7 Ops/sec
Script Preparation code:
var arr = []; for(var i=0; i < 10000; i++) { arr.push(Math.floor(Math.random() * Math.floor(10000))); }
Tests:
for of
var sum = 0; for (var e of arr) { sum += e; }
forEach
var sum = 0; arr.forEach(e => {sum += e;})
reduce
var sum = arr.reduce((acm, current) => acm + current);
for i
var sum = 0; for (var i = 0; i < arr.length; i++) { sum += arr[i]; }