Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For-of vs While - Random v1.2
(version: 4)
Comparing performance of:
forofreturn vs whileloopindex
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var variants = [{ id: 1, weight: 1}, { id: 2, weight: 10 }, { id: 3, weight: 10 }, { id: 4, weight: 10 }, { id: 5, weight: 10 }]; var totalWeight = variants.reduce((sum, item) => sum + item.weight, 0); var scores = Array.from({ length: 10 }).map(() => { Math.trunc(Math.random() * totalWeight) }); var forofreturn = (score) => { for (const variant of variants) { if (score < variant.weight) { return variant; } score -= variant.weight; } } var whileloopindex = (score) => { var index = 0; while (score > 0) { score -= variants[++index].weight; } return variants[index]; }
Tests:
forofreturn
forofreturn(scores[0]); forofreturn(scores[1]); forofreturn(scores[2]); forofreturn(scores[3]); forofreturn(scores[4]); forofreturn(scores[5]); forofreturn(scores[6]); forofreturn(scores[7]); forofreturn(scores[8]); forofreturn(scores[9]);
whileloopindex
whileloopindex(scores[0]); whileloopindex(scores[1]); whileloopindex(scores[2]); whileloopindex(scores[3]); whileloopindex(scores[4]); whileloopindex(scores[5]); whileloopindex(scores[6]); whileloopindex(scores[7]); whileloopindex(scores[8]); whileloopindex(scores[9]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forofreturn
whileloopindex
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 benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The benchmark measures the performance of two approaches: `for-of` loop and `while` loop with an index variable, on a dataset of 10 random scores generated from a weighted distribution. The weights are defined in the `Script Preparation Code`. **Script Preparation Code** The script preparation code defines: 1. An array of variants with their corresponding weights. 2. A total weight calculated by summing up all variant weights. 3. An array of 10 random scores generated by multiplying the total weight with a random number between 0 and 1. The `for-of` loop (`forofreturn`) function iterates through the variants array, returning the first variant whose weight is less than or equal to the current score. The `while` loop with an index variable (`whileloopindex`) function iterates through the variants array, subtracting each variant's weight from the score and using the index of the remaining score to return the corresponding variant. **Individual Test Cases** There are two test cases: 1. `forofreturn(scores[0])`, `forofreturn(scores[1])`, ..., `forofofscores[9]` This test case measures the performance of the `for-of` loop on each individual score. 2. `whileloopindex(scores[0])`, `whileloopindex(scores[1])`, ..., `whileloopindex(scores[9])` This test case measures the performance of the `while` loop with an index variable on each individual score. **Libraries and Features** None of the provided code uses any external libraries. However, it does utilize a few special JavaScript features: * The `reduce()` method is used to calculate the total weight. * The `Array.from()` method is used to generate the array of random scores. * The `Math.trunc()` function is used to truncate the result of multiplying the total weight with a random number. **Pros and Cons** The choice between the two approaches depends on the specific use case. Here are some pros and cons of each approach: * **For-of Loop** + Pros: - More concise and expressive code. - Easier to read and maintain. + Cons: - May be slower due to the overhead of iterating through the array. * **While Loop with Index Variable** + Pros: - Can be faster for large datasets, as it avoids the overhead of iterating through an array. + Cons: - More verbose and less expressive code. - Requires manual index management. **Other Alternatives** Other alternatives to these two approaches include: * Using a `forEach()` method instead of a `for-of` loop. * Using a `do-while` loop instead of a `while` loop with an index variable. * Using a `for` loop with an iterator object instead of a `for-of` loop. However, the choice of alternative will depend on the specific requirements and constraints of the use case.
Related benchmarks:
map vs forEach vs for loop
for..of
javascript loops (foreach, for-in, for-of, for, map, reduce) 1000 items
javascript loops with reduce 3
Comments
Confirm delete:
Do you really want to delete benchmark?