Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
generator vs array
(version: 0)
Comparing performance of:
generator vs array
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
generator
function* range(n) { var i; for (i = 0; i < n; i++) { yield n; } } let total = 0; for (let ii of range(100000)) { total += ii; }
array
function range(n) { let arr = []; for (i = 0; i < n; i++) { arr.push(i); } return arr; } let total = 0; for (let ii of range(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:
27 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
generator
1133.9 Ops/sec
array
376.2 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 Definition** The benchmark is comparing two approaches: using a generator and using an array to generate a sequence of numbers. **Options Compared** * Generator (using `function*` syntax) * Array (using traditional array creation and iteration) **Pros and Cons of Each Approach** Using Generators: Pros: * Memory efficiency, as only the current value is stored in memory * Can be used for asynchronous iterations * Can be paused and resumed at specific points Cons: * May have performance overhead due to the use of the `yield` keyword * Limited support in older browsers and JavaScript engines Using Arrays: Pros: * Wide support across modern browsers and JavaScript engines * Easy to create and manipulate arrays * Familiar syntax for many developers Cons: * Higher memory usage, as entire array is stored in memory * Can be slower due to the need to iterate over the entire array **Library/Function Used** In this benchmark, no libraries or external functions are used. The generator function is a built-in JavaScript feature. **Special JS Features/Syntax** The benchmark uses the `function*` syntax for generators, which was introduced in ECMAScript 2015 (ES6). This allows developers to create generator functions that can be used to iterate over sequences of values. **Alternative Approaches** Other approaches to generating sequences could include: * Using a `for...of` loop with an array expression * Using the `Array.prototype.map()` and `Array.prototype.reduce()` methods * Using a third-party library or framework for generating sequences However, these alternatives are not tested in this benchmark. **Benchmark Preparation Code** The preparation code is empty for both test cases, which means that no additional setup or initialization is required before running the benchmarks. **Latest Benchmark Result** The latest benchmark result shows that the array approach outperforms the generator approach. The Opera browser execution rate for the array test is higher than the generator test, indicating that arrays may be faster in this specific scenario. However, it's essential to note that performance can vary depending on the specific use case and hardware configuration.
Related benchmarks:
Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from() vs new Array().map()
Array.from() vs new A
Array.from() vs new Array() with index
Comments
Confirm delete:
Do you really want to delete benchmark?