Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of vs for cached 2
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of vs for cached
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1000);
Tests:
for
for (var i = 0; i < array.length; i++) { array[i]; }
foreach
array.forEach(function(e) { e; });
for in
for (var e in array) { e; }
for..of
for (var e of array) { e; }
for cached
const arrayLength = array.length; for (var i = 0; i < arrayLength; i++) { 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
for cached
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
9718.9 Ops/sec
foreach
721908.1 Ops/sec
for in
841701.8 Ops/sec
for..of
1854028.2 Ops/sec
for cached
20191.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The goal of this benchmark is to compare the performance of different loop constructs in JavaScript: 1. Traditional `for` loop 2. `forEach` loop (using the Array.prototype.forEach() method) 3. `for...in` loop (using the `for...in` statement with an array as its value) 4. `for...of` loop (using the `for...of` statement with an array as its value) 5. "Cached" `for` loop (using a cached variable for iteration) **Library and Special JS Features** None of the test cases explicitly use any external libraries or special JavaScript features other than built-in methods and syntax. **Loop Comparisons** The benchmark compares the performance of each loop type, comparing execution rates per second: * **Traditional `for` loop**: Iterates over the array using a manual index variable (`i`). Pros: Direct control over iteration. Cons: Can lead to off-by-one errors or index out-of-bounds issues if not managed carefully. * **`forEach` loop**: Uses the Array.prototype.forEach() method, which is designed for iterating over arrays. Pros: Easy to use and maintain. Cons: May have slower performance compared to manual indexing due to method overhead. * **`for...in` loop**: Iterates over the array's properties using a `for...in` statement with an array as its value. This is not a typical use case for `for...in`, which is designed for iterating over object property names. Pros: None notable. Cons: May have slower performance due to unnecessary iteration of object properties. * **`for...of` loop**: Iterates over the array's elements using a `for...of` statement with an array as its value. Pros: Easy to use and maintain. Cons: Limited control over iteration, as it relies on ES6+ features. * **"Cached" `for` loop**: Uses a cached variable for iteration, which can improve performance by reducing the overhead of assigning new values to the index variable. Pros: Potential performance improvement due to reduced indexing overhead. Cons: May lead to confusion if not managed carefully. **Benchmark Results** The latest benchmark results show that: * **`for...of` loop**: Outperforms other loop types, likely due to its optimized execution path and built-in support for array iteration. * **`forEach` loop**: Has slightly lower performance compared to the `for...of` loop but is still relatively efficient. * **Traditional `for` loop** and **"Cached" `for` loop**: Have slower performance, possibly due to the overhead of indexing or caching. Keep in mind that these results are specific to this benchmark and may not reflect real-world scenarios where other factors like array size, iteration complexity, or system configuration might impact performance. **Alternatives** Other alternatives for iterating over arrays in JavaScript include: * Using `Array.prototype.reduce()` or `Array.prototype.map()` methods * Implementing custom iteration logic using `while` loops or recursion * Leveraging third-party libraries or frameworks that provide optimized array iteration mechanisms However, the traditional loop constructs and `for...of` loop remain widely used and recommended due to their simplicity and readability.
Related benchmarks:
for cached vs foreach vs some vs for..of
for vs foreach for (cached length) vs for..of
for (cache length) vs foreach vs for..in vs for..of
for vs foreach vs for..in vs for..of vs for cached
Comments
Confirm delete:
Do you really want to delete benchmark?