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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser:
Chrome 142
Operating system:
Windows
Device Platform:
Desktop
Date tested:
9 months ago
reduce
5.2M/s
for
1.1M/s
for..of
892K/s
foreach
406K/s
View exact numbers
Test name
Executions per second
✓
reduce
5,196,232 Ops/sec
for
1,053,758 Ops/sec
for..of
891,798 Ops/sec
foreach
406,398 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;