Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
for vs foreach vs reduce vs for..of
Compare loop performance
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:145.0) Gecko/20100101 Firefox/145.0
Browser:
Firefox 145
Operating system:
Mac OS X 10.15
Device Platform:
Desktop
Date tested:
8 months ago
for
6.6M/s
reduce
3.6M/s
for..of
933K/s
foreach
751K/s
View exact numbers
Test name
Executions per second
✓
for
6,598,679 Ops/sec
reduce
3,633,659 Ops/sec
for..of
933,283 Ops/sec
foreach
750,907 Ops/sec
Script Preparation code:
var array = Array.from({ length: 200 }, () => { return Math.floor(Math.random() * (32000 - 0 + 1)) + 0; })
Tests:
for
let result = 0; for (var i = 0; i < array.length; i++) { result += array[i]; } result;
foreach
let result = 0; array.forEach(function(i) { result += array[i]; }); result;
reduce
let result = array.reduce((accumulator, currentValue) => { return accumulator + currentValue; }, 0); result;
for..of
let result = 0; for (var i of array) { result += array[i]; } result;