Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for in vs for of vs forEach (no length)
(version: 0)
Put array length outside of the loop
Comparing performance of:
forEach vs for of vs for in vs for
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [... new Array(1000)].map((x,i) => i);
Tests:
forEach
let result = 0; data.forEach(x => { result = x; });
for of
let result = 0; for (let x of data) { result = x; }
for in
let result = 0; for (let i in data) { result = data[i]; }
for
let result = 0; let len = data.length for (let i = 0; i < len; i++) { result = data[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
for of
for in
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/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
4085922.8 Ops/sec
for of
4325376.5 Ops/sec
for in
80595.4 Ops/sec
for
1140574.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases, explaining what is tested, the pros and cons of different approaches, and other considerations. **Benchmark Definition Overview** The benchmark measures the performance difference between four loop iteration methods: `for`, `for in`, `forEach` (without using the length property), and `for of`. The goal is to determine which method is the most efficient for iterating over an array. **Test Cases** Each test case represents a specific iteration method: 1. **`for`**: A traditional loop that uses a counter variable (`i`) to iterate over the array. 2. **`for in`**: An older, less efficient method that uses the `in` keyword to access array elements by their index. 3. **`forEach` (no length)**: The modern `Array.prototype.forEach()` method without using its `length` property for iteration. 4. **`for of`**: A more recent feature in JavaScript that allows using a `for...of` loop with arrays. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`for`**: Pros: * Widely supported across older browsers. * Easy to understand and implement. * Less memory-intensive. 2. **`for in`**: Pros: * Still used by some browsers for compatibility reasons. * Simple to implement, but not recommended due to inefficiencies. 3. **`forEach` (no length)**: Pros: * Modern, efficient method using the `Array.prototype.forEach()` method. * Less memory-intensive than traditional loops. 4. **`for of`**: Pros: * Efficient and modern method specifically designed for arrays. * Easy to read and understand. Cons: 1. **`for in`**: * Inefficient due to unnecessary type checking. * May not be supported by older browsers. 2. **`forEach` (no length)`:** * Some older JavaScript engines might not optimize this method correctly. 3. **`for of`**: * May not be supported by older browsers or JavaScript versions. **Library and Special JS Features** None of the test cases rely on specific libraries, but some browsers might use their own optimizations or caching mechanisms that affect the benchmark results. There are no special JavaScript features used in this benchmark (e.g., `let` with a block scope, arrow functions). **Alternatives** Other alternatives for iterating over arrays include: 1. **`for...of`**: A more modern and efficient method specifically designed for arrays. 2. **`Array.prototype.map()`**, **`Array.prototype.filter()`, or **`Array.prototype.reduce()`**: These methods can be used in combination with `forEach` to perform operations on array elements. Keep in mind that the choice of iteration method depends on the specific use case, personal preference, and compatibility requirements. **Conclusion** The benchmark measures the performance difference between four common loop iteration methods. The results show that `for of` is generally the most efficient method for iterating over arrays, followed by `forEach` (without using the length property). Understanding the pros and cons of each approach can help developers choose the best method for their specific use cases.
Related benchmarks:
for vs map
map vs forEach Chris
map vs forEach Chris v2
for vs foreach vs map 2
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?