Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of (updated)
(version: 1)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of
Created:
one year ago
by:
Guest
Go to the latest result
Script Preparation code:
const array = new Array(100);
Tests:
for
const LENGTH = array.length; for (let i = 0; i < LENGTH; i++) { array[i] = Math.random(); }
foreach
array.forEach(function(i) { array[i] = Math.random(); });
for in
for (let i in array) { array[i] = Math.random(); }
for..of
for (let i of array) { array[i] = Math.random(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0
Browser/OS:
Firefox 115 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
for
779K/s
for in
36K/s
for..of
24K/s
foreach
24K/s
View exact numbers
Test name
Executions per second
✓
for
778,741 Ops/sec
for in
35,754 Ops/sec
for..of
24,240 Ops/sec
foreach
23,552 Ops/sec
Related benchmarks
for vs foreach vs some
for w/length constant vs foreach vs some vs for..of
for vs foreach vs for..in vs for..of (Fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?