Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of sum
(version: 1)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of vs cached for
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100).fill(1);
Tests:
for
let sum = 0; for (let i = 0; i < array.length; i++) { sum += array[i]; }
foreach
let sum = 0; array.forEach(value => { sum += value; });
for in
let sum = 0; for (let i in array) { sum += array[i]; }
for..of
let sum = 0; for (let value of array) { sum += value; }
cached for
let sum = 0, len = array.length; for (let i = 0; i < len; i++) { sum += array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
cached 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/132.0.0.0 YaBrowser/25.2.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
4783004.0 Ops/sec
foreach
6698160.0 Ops/sec
for in
541491.6 Ops/sec
for..of
12056550.0 Ops/sec
cached for
6856605.5 Ops/sec
Related benchmarks:
for vs for...in vs forEach
for vs forEach vs for-of vs for-reverse vs reduce (calculate sum)
for/for each/forEach array sum
for/for in/forEach array sum
for/for in/forEach array sum 222
var for/for in/forEach array sum
var len for/for in/forEach array sum
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
Comments
Confirm delete:
Do you really want to delete benchmark?