Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
range generator vs array vs for
(version: 0)
Comparing performance of:
generator vs array vs for
Created:
2 years ago
by:
Guest
Go to the latest result
Tests:
generator
function* range(a, b) { do { yield a; } while ((a += 1) < b); } let total = 0; for (let ii of range(0,100000)) { total += ii; }
array
function range(a, b) { let arr = []; for (i = a; i < b; i++) { arr.push(i); } return arr; } let total = 0; for (let ii of range(0, 100000)) { total += ii; }
for
function range(a, b) { let arr = []; return arr; } let total = 0; for (i = 0; i < 100000; i++) { total += i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
generator
array
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
for
730/s
array
432/s
generator
361/s
View exact numbers
Test name
Executions per second
✓
for
730 Ops/sec
array
432 Ops/sec
generator
361 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is defined by a script that generates an array of numbers using three different approaches: `range` generator, array creation, and traditional for loop. The script also calculates the sum of these numbers. **Approaches** 1. **Generator**: A function that uses a generator to produce a sequence of numbers from 0 to 100000. The generated values are then added up. 2. **Array Creation**: A function that creates an array with numbers from 0 to 100000 using a for loop and the `push` method to add elements to the array. The sum is calculated after creating the array. 3. **Traditional For Loop**: A traditional for loop without arrays, where each iteration increments a counter variable (`i`) and adds its value to a running total. **Pros and Cons** * **Generator**: Pros: * Efficient memory usage since it doesn't create an array with all values. * Fast execution because it only generates values as needed. * Cons: * May have slower initialization time due to generator setup. * **Array Creation**: Pros: * Fast initialization time because the array is created upfront. * Efficient memory usage once the array is created. * Cons: * Creates an array with all values, potentially leading to high memory usage for large inputs. * May be slower execution speed due to pushing elements to an array. * **Traditional For Loop**: Pros: * Fast and simple implementation. * No memory overhead since it doesn't create a data structure. * Cons: * Slower initialization time because the loop counter needs to be initialized. * Slow execution speed due to repeated additions inside the loop. **Library Used** None of the approaches explicitly use any JavaScript libraries beyond the built-in `Array` and `Math`. **Special JS Feature/Syntax** No special JavaScript features or syntax are used in these approaches. They primarily rely on standard iteration constructs like generators, for loops, and array methods. **Other Alternatives** For generating a range of numbers, other alternatives could include: * Using `Array.from()` with an iterator function. * Employing `Map` or `Set` objects to accumulate the sum (though this might be slower than traditional arrays). * Applying recursive functions to generate the sequence. However, these approaches would likely exhibit different performance characteristics and memory usage patterns compared to the ones described in the benchmark.
Related benchmarks
Array.from() vs new Array()
Array.from({ length: n }) vs new Array(n)
Array() vs Array.from() fill
Comments
Confirm delete:
Do you really want to delete benchmark?