Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs Foreach vs Do While vs While
(version: 0)
See who is faster
Comparing performance of:
For vs Foreach vs Do While vs While
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
For
for (var i = 0; i < array.length; i++) { array[i]; }
Foreach
array.forEach(function(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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For
3485926.0 Ops/sec
Foreach
5718323.5 Ops/sec
Do While
5500375.5 Ops/sec
While
5587303.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different loop constructs in JavaScript is an essential aspect of understanding how to optimize code for various browsers and devices. **What is being tested:** The provided benchmark measures the execution time of four loop constructs: 1. `for` loop 2. `forEach` loop (part of the ECMAScript standard) 3. `do-while` loop 4. `while` loop These loops are used to iterate over an array and perform some operation on each element. **Options compared:** The benchmark compares the execution time of these four loop constructs on a specific input: * An array of size 100, initialized with a certain amount of data. * Each loop construct is executed until the entire array has been processed. **Pros and Cons of different approaches:** * **For loop**: This is a traditional loop construct that uses an explicit counter variable to iterate over the array. It's widely supported by most browsers and devices, but can be slower for certain use cases due to its inherent overhead. * Pros: Simple, widely supported, easy to read and write. * Cons: Can be slower than other constructs due to its explicit counter variable. * **ForEach loop**: This is a modern loop construct introduced by ECMAScript standard (ES6). It's designed to simplify iteration over arrays and is optimized for performance. However, its syntax can look different from traditional loops. * Pros: Optimized for performance, concise syntax, widely supported by modern browsers and devices. * Cons: Syntax may look unfamiliar to some developers, not widely supported in older browsers or devices. * **Do-while loop**: This is a variation of the traditional `for` loop with an initial counter value. It's designed to allow for more flexibility when initializing the loop counter. However, its syntax can make it harder to understand for new developers. * Pros: Can simplify initialization logic, more flexible than traditional `for` loops. * Cons: Syntax may be unfamiliar or harder to read for some developers, less optimized for performance compared to other constructs. * **While loop**: This is a general-purpose loop construct that allows the developer to control the loop termination condition. It's widely supported by most browsers and devices but can lead to more complex code due to its flexibility. **Library usage:** None of the individual test cases use any external libraries or frameworks, as they are designed to demonstrate basic JavaScript execution performance. **Special JS feature/syntax:** The benchmark makes no specific claims about using modern JavaScript features like `async/await`, Promises, or Web Workers, as these features are not essential to the fundamental nature of loop execution time comparisons. **Other alternatives:** If you're interested in exploring alternative loop constructs or optimizing your code for better performance: * **Generator loops**: Generator functions (ECMAScript 2015+) can provide a more efficient way to iterate over arrays using `yield` statements. * **Async loops**: Asynchronous loops like `async/await` can be used in conjunction with promises or callbacks to simplify I/O-bound operations and improve performance. In summary, this benchmark provides a straightforward comparison of the execution time of different loop constructs in JavaScript. The results help developers choose the most efficient construct for their specific use cases.
Related benchmarks:
Array fill foreach, vs for i loop
For vs Foreach vs Do While vs While v3
Array fill map, vs for i loop
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?