Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs Foreach vs Do While vs While (array length in variable)
(version: 1)
See who is faster
Comparing performance of:
For vs Foreach vs Do While vs While
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
For
let arrLength = array.length; for (var i = 0; i < arrLength; i++) array[i];
Foreach
array.forEach((i)=> array[i]);
Do While
var i = 0; do { array[i]; i++; } while (i < 100);
While
var i = 0; while (i < 100) { array[i]; i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
For
Foreach
Do While
While
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 Edg/130.0.0.0
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For
9649582.0 Ops/sec
Foreach
12575770.0 Ops/sec
Do While
9170519.0 Ops/sec
While
10161368.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided aims to compare the performance of four different looping methods in JavaScript when iterating over an array. The looping methods being tested are `for`, `forEach`, `do while`, and `while` loops. All iterations utilize an array of a fixed length (100) that is pre-defined in the preparation code. ### Comparison of Looping Methods 1. **For Loop:** - **Benchmark Definition:** ```javascript let arrLength = array.length; for (var i = 0; i < arrLength; i++) array[i]; ``` - **Pros:** - Generally efficient for most use cases because it allows manual control over the loop variable and the exit condition. - Can be faster in performance-critical applications, especially if the loop count is large. - **Cons:** - Less readable compared to higher-level abstractions like `forEach`. - Prone to off-by-one errors if not carefully managed. 2. **ForEach Method:** - **Benchmark Definition:** ```javascript array.forEach((i) => array[i]); ``` - **Pros:** - More readable and declarative; expresses the intention of iterating through elements. - No need to manage the loop variable manually. - **Cons:** - May have slightly worse performance in tight loops compared to traditional loops like `for` and `while`, as the function callback adds overhead. - Cannot use `break` or `continue` to manipulate the flow of the loop. 3. **Do While Loop:** - **Benchmark Definition:** ```javascript var i = 0; do { array[i]; i++; } while (i < 100); ``` - **Pros:** - Guarantees that the loop body will be executed at least once, which is useful for certain scenarios. - **Cons:** - Similar in performance to the `while` loop, but less commonly used. - The initial condition must be managed carefully to prevent infinite loops. 4. **While Loop:** - **Benchmark Definition:** ```javascript var i = 0; while (i < 100) { array[i]; i++; } ``` - **Pros:** - Like `for`, it allows fine-tuned control over the iteration. - Can be more suitable than `for` for indefinite loops where the exit condition isn't based solely on a counter. - **Cons:** - Also prone to off-by-one errors if not implemented correctly. ### Benchmark Results The results from the most recent tests show performance measured in executions per second for each method. The highest-performing method was `forEach`, followed by `while`, `for`, and `do while`. Here are the execution rates: - **Foreach:** 12,575,770 executions/second - **While:** 10,161,368 executions/second - **For:** 9,649,582 executions/second - **Do While:** 9,170,519 executions/second ### Other Considerations - **Performance Variation:** Performance may vary between JavaScript engines (V8 for Chrome, etc.) due to optimizations that are applied to different types of loops. - **Use Case:** The choice of which loop to use should generally be guided more by readability and maintainability of the code rather than raw performance, unless performance is mission-critical. - **Alternatives:** Other looping constructs such as `map`, `filter`, and `reduce` also exist in JavaScript, which provide similar functionality with added capabilities (like transformed outputs) but come with their own performance trade-offs. In summary, while the benchmark illustrates performance differences among loop types, choices in production code should be made with an emphasis on clarity, maintainability, and context-driven performance concerns.
Related benchmarks:
for vs foreach vs some vs for..of vs for cached
for vs foreach vs some vs for..of vs while
for vs foreach for (cached length) vs for..of
for vs foreach for (cached length) vs for..of -- With assignment
for vs foreach vs some vs for..of
For vs Foreach vs Do While vs While
For vs Foreach vs Do While vs While v3
For vs Foreach vs While
for vs .foreach vs .some vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?