Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript loops2
(version: 3)
Comparing performance of:
foreach vs for-in vs for-of vs for
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
foreach
const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40)); let index = 0; items.forEach(i => index++);
for-in
const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40)); let index = 0; for (let i in items) { item = items[i] index++; }
for-of
const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40)); let index = 0; for (let i of items) { index++; }
for
const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40)); let index = 0; for(let i = 0; i < items.length; ++i) { item = items[i] index++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for-in
for-of
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
6.5 Ops/sec
for-in
3.6 Ops/sec
for-of
7.1 Ops/sec
for
4.7 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "javascript loops2" focuses on testing and comparing the performance of different loop constructs in JavaScript. In particular, it measures the execution speed of four common iterating patterns when used with an array containing 500,000 randomly generated items. The loops tested and their comparisons are as follows: ### Test Cases 1. **forEach**: ```javascript items.forEach(i => index++); ``` - This method calls a provided function once for each element in the array. - **Pros**: - Cleaner and more readable syntax. - Abstracts away the iteration logic, making it easier to reason about code. - **Cons**: - Generally slower than traditional for loops due to function invocation overhead. - Cannot be easily exited early using `break` or `return`. 2. **for-in**: ```javascript for (let i in items) { item = items[i]; index++; } ``` - This loop iterates over the enumerated properties of an object. - **Pros**: - Simple to write for objects with enumerable properties. - **Cons**: - Not recommended for arrays because it can iterate over inherited properties, which can lead to unexpected behavior. - Slower than other loop constructs for large arrays due to its object nature. 3. **for-of**: ```javascript for (let i of items) { index++; } ``` - This loop iterates over iterable objects (like arrays) and provides direct access to the elements. - **Pros**: - Intuitive syntax and straightforward use. - More readable than a traditional for loop while maintaining performance. - **Cons**: - Slight overhead compared to a traditional for loop, but generally performs well. 4. **for**: ```javascript for(let i = 0; i < items.length; ++i) { item = items[i]; index++; } ``` - A traditional for loop utilizing an index to control iterations. - **Pros**: - Usually the fastest among these options due to minimal overhead. - Allows full control of the iteration process, including early exits. - **Cons**: - Slightly more verbose, which can lead to less readable code if overused. - Requires manual index management. ### Results of Benchmark From the benchmark results, the loop performance can be observed as follows: 1. **for-of**: 7.11 executions per second 2. **forEach**: 6.53 executions per second 3. **for**: 4.67 executions per second 4. **for-in**: 3.62 executions per second ### Considerations and Other Alternatives - **Execution Speed**: The traditional `for` loop, while slightly slower in this benchmark, is often considered the go-to for performance-critical scenarios due to direct access to array elements. In many cases, optimizing JavaScript for performance requires careful consideration of the loop constructs. - **Readability vs. Performance**: There is often a trade-off between code readability and performance. While `forEach` and `for-of` offer a more modern and clutter-free approach, they might sacrifice execution speed. - **Using Libraries**: The benchmark does not utilize any external libraries, but developers sometimes turn to utility libraries like Lodash or Underscore.js for more complex operations on collections, which can offer enhanced functionality (e.g., chaining, immutability) but at the cost of additional overhead. In conclusion, when choosing a loop type in JavaScript, developers should consider not only the performance characteristics as revealed by such benchmarks but also code clarity, maintainability, and the specific needs of their application.
Related benchmarks:
javascript loops
javascript ruletz
javascript loops small
javascript loops (foreach, for-in, for-of, for, map, reduce) 1000 items
javascript loops with reduce 3
javascript loops with reduce 4
javascript loops with reduce 5
javascript loops (bigger loops)
javascript loops 10000000 items
Comments
Confirm delete:
Do you really want to delete benchmark?