Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
While vs For Loop - Luigi
(version: 0)
Comparing performance of:
For loop vs While loop
Created:
one year ago
by:
Guest
Go to the latest result
Tests:
For loop
const size = 100000 const generateRandomDigit = () => Math.floor(Math.random() * 10) const generateRandomLetter = () => String.fromCharCode(Math.floor(Math.random() * 26) + 65) for (var i = 0; i < size; i++) { generateRandomDigit() generateRandomLetter() }
While loop
const size = 100000 const generateRandomDigit = () => Math.floor(Math.random() * 10) const generateRandomLetter = () => String.fromCharCode(Math.floor(Math.random() * 26) + 65) let c = 0 do { c++ generateRandomDigit() generateRandomLetter() } while(c <= size)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For loop
While loop
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; rv:131.0) Gecko/20100101 Firefox/131.0
Browser/OS:
Firefox 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
While loop
183/s
For loop
168/s
View exact numbers
Test name
Executions per second
✓
While loop
183 Ops/sec
For loop
168 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two different loops: a `for` loop and a `while` loop. The loops are designed to generate random digits and letters, which is likely chosen because it's a simple and quick operation that can be executed repeatedly. **Loop Options Compared** There are two options being compared: 1. **For Loop**: This loop uses the traditional syntax: `for (var i = 0; i < size; i++) { ... }`. The loop iterates over a fixed range of values (`size`) and executes the code inside the loop for each value. 2. **While Loop**: This loop uses the conditional syntax: `do { c++; generateRandomDigit(); generateRandomLetter(); } while(c <= size)`. The loop continues to execute as long as the condition `c <= size` is true. **Pros and Cons of Each Approach** * **For Loop**: + Pros: Easy to read and understand, allows for explicit control over iteration variables. + Cons: Can be less efficient than `while` loops in some cases, since it requires a fixed range of values. * **While Loop**: + Pros: Can be more efficient than `for` loops when the number of iterations is unknown or large, as it only iterates until the condition is false. + Cons: Can be less readable and easier to mistake due to the conditional syntax. **Library Usage** There are no libraries explicitly mentioned in the benchmark definitions. However, the code uses some built-in JavaScript functions: * `Math.random()`: generates a random number between 0 (inclusive) and 1 (exclusive). * `String.fromCharCode()`: returns a string created from the specified Unicode character. * `++` operator: increments the variable `c` by 1. **Special JS Feature/Syntax** The benchmark uses some JavaScript features that are not essential to understanding the code: * The `const` keyword is used to declare variables, which is a modern feature introduced in ECMAScript 2015 (ES6). * The arrow function syntax (`() => { ... }`) is also used, but it's not specific to this benchmark. * The `let` keyword is used to declare the variable `c`, which is a more modern alternative to the traditional `var` keyword. **Other Alternatives** There are other loop options available in JavaScript, such as: * **Recursive loops**: use function calls to iterate over values (not typically recommended due to performance overhead). * **Array-based loops**: use array methods like `forEach()` or `reduce()` to iterate over values. * **Generator functions**: can be used to create iterators and iterate over values. However, these alternatives are not explicitly mentioned in the benchmark definitions.
Related benchmarks
endsWith() vs Ordinary For loop
Comments
Confirm delete:
Do you really want to delete benchmark?