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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser:
Chrome 120
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
for of
260K/s
reduce
204K/s
forEach
158K/s
for i
2K/s
View exact numbers
Test name
Executions per second
✓
for of
259,721 Ops/sec
reduce
203,890 Ops/sec
forEach
157,979 Ops/sec
for i
1,501 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]; }