Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of vs for..in Kyran
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..in
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i; i < 1000; i++) array[i] = i; var powSum = 0;
Tests:
for
for (var index = 0; index < array.length; index++) { powSum += Math.pow(array[index], 2); } console.log(powSum)
foreach
array.forEach((value) => { powSum += Math.pow(value, 2); }); console.log(powSum)
for..of
for (const value of array) { powSum += Math.pow(value, 2); } console.log(powSum)
for..in
for (const index in array) { powSum += Math.pow(array[index], 2); } console.log(powSum)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for..of
for..in
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
foreach vs for..of
for vs foreach vs for..of (aprudnikov)
.forEach vs for const of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?