Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript loops v2
(version: 1)
Comparing performance of:
foreach vs for-in vs for-of vs for vs optimized-for
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
foreach
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; items.forEach(i => index += items[i]);
for-in
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for (let i in items) index += items[i];
for-of
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for (let i of items) index += i;
for
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for(let i = 0; i < items.length; ++i) index += items[i];
optimized-for
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; let i = items.length; while (i--) index += items[i];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
foreach
for-in
for-of
for
optimized-for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
29044.2 Ops/sec
for-in
24133.0 Ops/sec
for-of
36487.3 Ops/sec
for
36439.2 Ops/sec
optimized-for
36760.9 Ops/sec
Related benchmarks:
javascript loops
javascript loops small
javascript loops (foreach, for-in, for-of, for, map, reduce) 1000 items
javascript loops with reduce 3
javascript loops with reduce 4
javascript loops with reduce 5
javascript loops (bigger loops)
javascript loops 10000000 items
javascript loops2
Comments
Confirm delete:
Do you really want to delete benchmark?