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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser:
Chrome 128
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
for of
158769.2 Ops/sec
forEach
60427.2 Ops/sec
reduce
91042.8 Ops/sec
for i
1699.4 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]; }