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 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0
Browser:
Firefox 127
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
for of
15692.3 Ops/sec
forEach
20680.9 Ops/sec
reduce
22597.9 Ops/sec
for i
98355.8 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]; }