Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs do while
(version: 0)
Comparing performance of:
for vs do while
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array(1000).fill(0).map((v, i) => i);
Tests:
for
var copy = data.slice(0, 100); data.forEach((dane) => console.log(dane))
do while
var i = 0; do { console.log(i) i++; } while(i < 100)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
do while
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
142.3 Ops/sec
do while
1446.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark. **What is tested:** The provided JSON represents two JavaScript microbenchmarks that compare the performance of two different loops: 1. A traditional `for` loop 2. A `do-while` loop In each test case, a large array (`data`) is created and logged using either a `for` or `do-while` loop. **Options compared:** The benchmark compares the performance of the following options: * Traditional `for` loop: `var i = 0; for (i < data.length; i++) { console.log(data[i]); }` * `do-while` loop: `var i = 0; do { console.log(i); i++; } while (i < data.length)` **Pros and Cons of each approach:** 1. Traditional `for` loop: * Pros: + More concise and readable code + Easier to manage loop variables * Cons: + May not be as efficient due to the overhead of the conditional check at the end of each iteration 2. `do-while` loop: * Pros: + Can be more efficient because it avoids the overhead of a conditional check at the end of each iteration * Cons: + Less concise and less readable code compared to traditional `for` loops **Other considerations:** In addition to the loop structure, there are other factors that can affect performance, such as: * Array indexing vs. array slicing (not explicitly tested in this benchmark) * Loop variable initialization vs. reinitialization (not explicitly tested in this benchmark) **Library usage:** None of the test cases use any external libraries. **Special JS feature or syntax:** There are no special JavaScript features or syntax used in these test cases. **Benchmark preparation code:** The `Script Preparation Code` is used to create a large array (`data`) that will be logged using the tested loop structures. The code uses the `Array.prototype.fill()` and `Array.prototype.map()` methods, which are modern JavaScript methods introduced in ECMAScript 2015 (ES6). **Other alternatives:** There are other ways to achieve the same result as these test cases, such as: * Using a `while` loop with an explicit counter variable * Using a recursive function to iterate over the array * Using a library like Lodash or Ramda for functional programming However, these alternatives may not provide meaningful comparisons to the traditional `for` and `do-while` loops.
Related benchmarks:
for vs foreach vs map 2
Array fill method vs for loop vs map
Array fill map, vs for i loop
Array fill map, vs while loop
fill array with value: map(callback) vs fill(value) 2
Comments
Confirm delete:
Do you really want to delete benchmark?