Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Looping
(version: 0)
for loop or for...of with a generator?
Comparing performance of:
for loop vs for...of range
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function* range(from, to, step=1) { while ((from += step) < to) yield from; }
Tests:
for loop
for (let i = 0; i < 10; ++i) console.log(i);
for...of range
for (const i of range(0, 10)) console.log(i);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
for...of range
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 benchmark and its components. **Benchmark Definition JSON** The benchmark is defined in two parts: `Script Preparation Code` and `Html Preparation Code`. The former specifies a JavaScript function called `range` that generates an array of numbers from 0 to 10 with a specified step. This function uses a generator (`function*`) to produce the values. The latter, `Html Preparation Code`, is empty in this case, which means no HTML-related code is executed before running the benchmark. **Options Compared** Two options are being compared: 1. **For loop**: A traditional, imperative loop that increments a counter variable from 0 to 9 and logs each value to the console. 2. **For...of with a generator (range)**: A more functional approach using the `range` function defined in the script preparation code. This uses a generator to produce values instead of manually incrementing a counter. **Pros and Cons** **For Loop** Pros: * Wide support across browsers and platforms * Easy to understand and implement for developers familiar with traditional loops Cons: * May be slower due to the overhead of manual incrementation and comparison operations **For...of with Generator (Range)** Pros: * Can be faster, as the generator eliminates the need for manual incrementation and comparison * More concise and expressive code for functional programming enthusiasts Cons: * May require additional setup or familiarity with generators for some developers * Support might vary across browsers and platforms **Other Considerations** * **Generator Syntax**: The `range` function uses a generator (`function*`) to produce values. This is a feature introduced in ECMAScript 2015 (ES6) that allows functions to return an iterable value. Not all JavaScript engines support generators natively, but most modern browsers do. * **Library Usage**: In this case, the `range` function uses a library-like syntax (`function*`) without importing any external libraries. However, if you were to write similar code using a custom loop or manual incrementation, it might involve more explicit library calls. **Alternative Approaches** Other ways to implement a loop in JavaScript could include: 1. **Using `Array.prototype.forEach`**: This method iterates over an array and executes a callback function for each element. 2. **Utilizing `Set` and `for...of`**: This approach uses a `Set` data structure to store unique values and iterates over it using the `for...of` loop. 3. **Implementing a custom loop with manual incrementation**: This involves writing explicit code to increment a counter variable and compare it against a threshold. Keep in mind that each of these alternatives has its own trade-offs, pros, and cons, which are dependent on specific use cases and performance requirements. For this particular benchmark, comparing the for loop and for...of with generator (range) approaches allows developers to evaluate which approach is faster and more efficient.
Related benchmarks:
Making Array Range
Nj9ZJ^FuK4AJ#wKE
range generator vs array
range generator vs array vs for
Comments
Confirm delete:
Do you really want to delete benchmark?