Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of vs for..of over entries vs for in
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..of over entries vs for..in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 100}); var t;
Tests:
for
for (let i = 0; i < array.length; i++) { t = array[i]; }
foreach
array.forEach(function(v, i) { t = v; });
for..of
for (var v of array) { t = v; }
for..of over entries
for (var [i, v] of array.entries()) { t = v; }
for..in
for (var k in array) { t = array[k]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
for..of
for..of over entries
for..in
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
78832.0 Ops/sec
foreach
79589.6 Ops/sec
for..of
78262.2 Ops/sec
for..of over entries
76072.1 Ops/sec
for..in
68624.8 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares the performance of different ways to iterate over an array in JavaScript. **Options Compared:** * **`for` loop:** The classic way to loop, using an index (`i`) to access each element. * **`forEach()` method:** A higher-order function that iterates over each element and calls a provided callback function. * **`for...of` loop:** A modern syntax for iterating directly over the values in an iterable (like an array). * **`for...of` over `entries()`:** This loops over the key-value pairs of the array using `entries()`. While useful, it's not the most common way to iterate over values alone. * **`for...in` loop:** Loops over the enumerable properties (keys) of an object. While it can work on arrays, it's generally less efficient and meant for objects. **Pros and Cons:** * **`for` loop:** Can be very performant if you need to access element indices, but verbose. * **`forEach()`:** Concise and readable, good for simple operations. Might have a slight performance overhead compared to `for...of`. * **`for...of`:** Most concise and often the fastest for simple iteration over values. * **`for...of` over `entries()`:** Useful if you need both index/value pairs, but adds extra processing. **Other Considerations:** * The benchmark results can vary based on factors like JavaScript engine optimization, browser versions, hardware, and even network conditions. Always interpret benchmarks with caution. * **Choosing the best approach:** Consider: * Do you need the element's index? (`for` loop or `forEach`) * Is simplicity most important? (`for...of`) * Are you working with objects or arrays? (Avoid `for...in` for arrays) Let me know if you have any more questions about JavaScript loops or performance testing!
Related benchmarks:
foreach vs for..of
Get index with forEach vs for...of over entries
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?