Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Range vs for of
(version: 0)
Comparing performance of:
for vs range
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function* range(lo, hi) { for (let i = lo; i < hi; i++) { yield i; } } window.arr = new Array(10000); for (const i of range(0, 10000)) { arr[i] = i; }
Tests:
for
let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; }
range
let sum = 0; for (const i of range(0, arr.length)) { sum += arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
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 provided JSON and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: traditional `for` loop and the `range()` function. * The `range()` function is a generator function that yields numbers from a specified start value (`lo`) up to, but not including, a stop value (`hi`). In this case, it's used to generate an array of numbers from 0 to 9,999. * The script preparation code initializes an array `arr` with 10,000 elements and assigns the generated numbers from `range()` to these elements. **Options Compared** There are two options being compared: 1. **Traditional `for` loop**: This is a standard way of iterating over an array in JavaScript. 2. **`range()` function**: A generator function that generates numbers on-the-fly, eliminating the need for explicit array creation and indexing. **Pros and Cons of Each Approach** * **Traditional `for` loop**: + Pros: Familiar syntax, easy to read and maintain. + Cons: Requires explicit array creation and indexing, can be slower due to overhead of accessing elements by index. * **`range()` function**: + Pros: Eliminates need for explicit array creation and indexing, potentially faster due to reduced overhead. + Cons: Less familiar syntax, may require more code to achieve the same result. **Library/Functionality Used** The `range()` function is a custom implementation, likely created for this benchmark. Its purpose is to generate numbers on-the-fly, allowing for dynamic array creation and indexing without the need for explicit array allocation. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. However, it's worth noting that generator functions like `range()` are a relatively new addition to JavaScript (introduced in ECMAScript 2015) and may not be supported by older browsers. **Other Alternatives** If the benchmark were to compare other approaches, some alternatives could include: * Using `Array.prototype.fill()` or `Array.prototype.set()` to create an array with initial values. * Utilizing a library like Lodash's `range` function for generating arrays. * Employing alternative iteration methods, such as using a `Set` or `Map` data structure instead of an array. Keep in mind that the choice of approach often depends on the specific requirements and constraints of the project.
Related benchmarks:
Looping
generator vs array
range generator vs array
range generator vs array vs for
Comments
Confirm delete:
Do you really want to delete benchmark?