Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs reduce vs for..of
(version: 1)
Compare loop performance
Comparing performance of:
for vs foreach vs reduce vs for..of
Created:
9 months ago
by:
Guest
Go to the latest result
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
reduce
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
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
Related benchmarks
for vs foreach vs for..of vs for..of over entries
for vs foreach vs for..of vs for..of over entries vs for in
for vs foreach vs for..in vs for..of zzz2
Sum array: for vs forEach vs for..in vs for..of vs reduce
2Sum array: for vs forEach vs for..in vs for..of vs reduce
for vs foreach vs for..in vs for..of (updated)
Comments
Confirm delete:
Do you really want to delete benchmark?