Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iteration Speed 2
(version: 2)
Comparing performance of:
while loop vs for loop vs do .. while loop vs forEach loop
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 50000; i++) { arr[i] = i; }
Tests:
while loop
let i = 0 while(i < arr.length) { arr[i] i++ }
for loop
for(let i = 0; i < arr.length; i++) { arr[i] }
do .. while loop
let i = 0 do { arr[i] i++ } while(i < arr.length)
forEach loop
arr.forEach((data) => { data })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
while loop
for loop
do .. while loop
forEach loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
while loop
28426.3 Ops/sec
for loop
33236.5 Ops/sec
do .. while loop
26654.0 Ops/sec
forEach loop
5206.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 the pros and cons of each approach. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The test measures the execution speed of different looping constructs in JavaScript: `while`, `for`, `do-while`, and `forEach`. **Script Preparation Code** The script preparation code is the same for all test cases: ```javascript var arr = []; for (var i = 0; i < 50000; i++) { arr[i] = i; } ``` This code initializes an array `arr` with 50,000 elements and assigns each element its index value. **Html Preparation Code** The html preparation code is empty for all test cases, which means that no HTML-related setup or teardown is performed before running the benchmark. **Looping Constructs** Each test case measures a different looping construct: 1. `while` loop: ```javascript let i = 0; while (i < arr.length) { arr[i]; i++; } ``` 2. `for` loop: ```javascript for (let i = 0; i < arr.length; i++) { arr[i]; } ``` 3. `do-while` loop: ```javascript let i = 0; do { arr[i]; i++; } while (i < arr.length); ``` 4. `forEach` loop: ```javascript arr.forEach((data) => { data; }); ``` **Comparison of Looping Constructs** The pros and cons of each looping construct are as follows: * `while` loop: + Pros: allows for more flexibility in the loop condition, can be used with arrays or other iterable objects. + Cons: requires manual incrementation of the index variable, can lead to off-by-one errors if not handled carefully. * `for` loop: + Pros: easier to read and write than `while` loops, allows for more control over the loop iteration. + Cons: less flexible than `while` loops, requires explicit initialization of the loop variables. * `do-while` loop: + Pros: similar to `while` loops but with an additional check at the end of each iteration. + Cons: can be harder to read and write, may not be as common in modern JavaScript code. * `forEach` loop: + Pros: concise and readable, allows for easy iteration over arrays or other iterable objects. + Cons: limited control over the loop iteration, may not be suitable for complex logic. **Library Usage** None of the test cases use any external libraries or dependencies. The looping constructs are used in isolation to compare their execution speeds. **Special JS Features/ Syntax** None of the test cases explicitly use special JavaScript features like async/await, generators, or decorators. However, the `for...of` loop (not shown in this example) is not mentioned either. **Alternative Approaches** Other alternatives for implementing looping constructs in JavaScript include: * Using `Array.prototype.map()`, `Array.prototype.filter()`, and other array methods to achieve similar results. * Utilizing third-party libraries like Lodash or Ramda for functional programming-related looping constructs. * Implementing custom looping constructs using recursive functions, iteration over arrays or objects, or using Web Workers. Keep in mind that this is a simplified example, and real-world benchmarking scenarios may involve additional complexity and nuance.
Related benchmarks:
For and Map
empty an array in JavaScript?(Yorkie)1
testtmptmptmp
while, for, for-of, map
Lodash.js vs Native22222yslysl2222
Comments
Confirm delete:
Do you really want to delete benchmark?