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:
6 months ago
by:
Guest
Jump 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:
5 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
Test name
Executions per second
for
6598679.0 Ops/sec
foreach
750906.8 Ops/sec
reduce
3633659.0 Ops/sec
for..of
933282.6 Ops/sec
Related benchmarks:
foreach vs for vs for (length var) vs for..in vs for..of
for vs foreach vs some vs for..of 1000 (improved)
for vs foreach vs for..of vs for..of over entries
for vs foreach vs for..of vs for..of over entries random
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?