Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
random String for loop vs while loop
(version: 0)
Comparing performance of:
for loop vs while loop
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
for loop
function randomString( length = 10, allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ) { let result = "" for (let i = 0; i < length; i++) { result += allowed.charAt(Math.floor(Math.random() * allowed.length)) } return result } randomString();
while loop
function foo( length = 10, allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ) { let result = "" let i=0; do { result += allowed.charAt(Math.floor(Math.random() * allowed.length)) i +=1; } while(i < length) return result } foo();
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is essentially a JavaScript function that generates a random string of a specified length using two different approaches: 1. **For Loop**: The `randomString` function uses a traditional for loop to iterate over a range of numbers, generating a random character from an allowed set. 2. **While Loop**: The `foo` function uses a do-while loop to achieve the same goal. **Options Compared** The two approaches being compared are: * Traditional for loops (using `for` keyword) * Do-while loops (using `do-while` keyword) **Pros and Cons** * **For Loops**: + Pros: More traditional, easier to understand, and well-supported by most browsers. + Cons: Can be slower due to the overhead of incrementing a variable using the `++` operator. * **Do-While Loops**: + Pros: Can be slightly faster since it avoids the overhead of incrementing a variable. + Cons: Less common in JavaScript, and some older browsers may not support them. **Library and Syntax** Neither of the two functions uses any external libraries. The syntax used is standard JavaScript with no advanced features like async/await or Promises. **Other Considerations** The benchmark measures the number of executions per second for each loop type on an iPhone device running Safari 16. This provides a relatively modern and typical web browser scenario. **Alternatives** If you wanted to test other loop types, you could consider adding additional test cases for: * Traditional while loops (without do-while syntax) * For-each loops or array methods like `map()` or `forEach()` * Other custom loop implementations Keep in mind that the choice of loop type can significantly impact performance and readability, so it's essential to choose the best approach for your specific use case.
Related benchmarks:
Compare TextEncoder, Blob, new TextEncoder
Encode vs Blob vs length
Blob vs TextEncoder
JS String '+' same v.s. different strings 2
Comments
Confirm delete:
Do you really want to delete benchmark?