Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
While vs For Loop - Luigi 3
(version: 0)
Comparing performance of:
For loop vs While loop
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
For loop
const size = 1000000 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 = 1000000 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
Test name
Executions per second
For loop
18.1 Ops/sec
While loop
18.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net! The provided benchmark measures the performance difference between two common looping constructs in JavaScript: `for` loops and `while` loops. The test is specifically designed to compare these two approaches when generating random digits and letters. **What is tested?** In this benchmark, we're testing the number of executions per second for each loop type on a desktop device running Firefox 131. The test case involves generating a large number (1,000,000) of random digits and letters using JavaScript functions `generateRandomDigit()` and `generateRandomLetter()`. We measure how many times these functions are executed within the specified loops. **Options compared:** There are two options being compared: 1. **For loop**: This is the traditional way to iterate over a block of code in JavaScript. It uses a counter variable (`i`) that increments on each iteration, and it includes an explicit termination condition. 2. **While loop**: This looping construct continues as long as a certain condition is true. In this test, the condition is `c <= size`, where `c` is the incrementing counter variable. **Pros and Cons of each approach:** 1. **For loop**: * Pros: + Easy to read and maintain + Typically faster due to less overhead in the loop control flow * Cons: + Can be less flexible, as the number of iterations is fixed during compilation 2. **While loop**: * Pros: + More flexible, as the condition can change or be dynamically determined * Cons: + Often slower due to additional overhead in the loop control flow **Library and purpose:** In this benchmark, we're using JavaScript's built-in `Math.random()` function, which is not a library per se. However, it's worth noting that some libraries might use optimized random number generation algorithms for better performance. **Special JS feature or syntax:** There are no special JavaScript features or syntax used in this test case. It's purely standard JavaScript code. **Other alternatives:** For loop and while loop are two of the most common looping constructs in JavaScript, but there are other alternatives: 1. **Do-while loop**: Similar to a `while` loop but executes at least once before checking the condition. 2. **foreach loop**: A more concise way to iterate over arrays or objects using an arrow function. In conclusion, this benchmark on MeasureThat.net helps us understand the performance differences between two common looping constructs in JavaScript: `for` loops and `while` loops. It's essential to consider these options when writing efficient code, as the choice of loop type can impact execution speed.
Related benchmarks:
Loop Test (forEach vs for)
while vs for
For vs Foreach vs Do While vs While
loop vs command
For vs Foreach vs Do While vs While v3
Comments
Confirm delete:
Do you really want to delete benchmark?