Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
range generator vs array
(version: 0)
Comparing performance of:
generator vs array
Created:
2 years ago
by:
Guest
Jump 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; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
generator
array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
generator
407.7 Ops/sec
array
731.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The test is comparing two approaches to generate an array of numbers from 0 to 100,000: using a traditional for loop with an array (`"array"`), and using a generator function (`"generator"`). **Options Compared** In this case, we have only two options: 1. **Traditional Array Generation**: This approach uses a for loop to iterate over the range of numbers, pushing each number onto an array. 2. **Generator Function**: This approach uses a generator function to yield numbers from 0 to 100,000. **Pros and Cons** **Traditional Array Generation:** * Pros: + Easy to understand and implement. + Well-documented and widely used. * Cons: + Creates an intermediate array in memory, which can be inefficient for large datasets. + Can lead to performance issues if the array is not properly managed. **Generator Function:** * Pros: + More memory-efficient than traditional array generation, as it only yields values on demand. + Can be more suitable for large datasets or infinite sequences. * Cons: + May require additional setup and understanding of generator functions. + Less widely supported or documented than traditional array generation. **Library/Function Used** In this benchmark, the `yield` keyword is used to define a generator function. The `do-while` loop is also used in conjunction with `yield`, which is an older syntax for generator functions. **Special JS Feature/Syntax** The test case uses a special JavaScript feature: **generators**. Generators are a type of function that can be used to generate a sequence of values, rather than computing them all at once and returning them as an array. This allows generators to work with large datasets without consuming excessive memory. **Other Alternatives** Some other alternatives to traditional array generation or generator functions include: * **NumJS**: A JavaScript library for numerical computations that provides efficient and optimized methods for array operations. * **Array.prototype.reduce()**: A method that can be used to generate an array by accumulating values in a reduction operation. * **Async/await** with **Arrays.from()**: An approach that uses async/await syntax to iterate over an array, which can provide better performance than traditional loops. These alternatives may offer different trade-offs in terms of complexity, performance, and memory usage.
Related benchmarks:
Array.from() vs new Array()
my Array.from() vs new Array()
Array.from({ length: n }) vs new Array(n)
Array.from() vs new A
Array.from() vs new Array() with index
Comments
Confirm delete:
Do you really want to delete benchmark?