Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
PHP - generator vs array
(version: 0)
Comparing performance of:
generator vs array
Created:
4 years ago
by:
Registered User
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:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
generator
439.7 Ops/sec
array
124.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition and Preparation Code** The provided JSON represents a benchmark test between two approaches: using a generator function (yield) versus an array to generate a sequence of numbers. * The `Script Preparation Code` is empty, which means that no specific script or setup code is required for this benchmark. * The `Html Preparation Code` is also empty, indicating that the benchmark does not rely on any HTML-related setup. * The `Name` and `Description` fields provide a human-readable name and description for the benchmark, respectively. **Test Cases** There are two individual test cases: 1. **Generator**: This test case uses a generator function (`function* range(n) { ... }`) to generate an array of numbers from 0 to `n`. The generated sequence is then iterated over using a `for...of` loop, and the sum of all elements is calculated. 2. **Array**: This test case uses a traditional array-based approach (`function range(n) { ... }`) to generate an array of numbers from 0 to `n`. The generated array is then iterated over using a `for...of` loop, and the sum of all elements is calculated. **Comparison** The two test cases compare the performance differences between using a generator function (yield) versus an array-based approach for generating sequences of numbers. * **Generator**: This approach uses a generator function to produce an iterable sequence of numbers. The `yield` keyword allows the function to produce a value and suspend its execution, which can be more memory-efficient than creating a large array. * **Array**: This approach creates a traditional array of numbers using the `push` method. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Generator (yield)**: + Pros: Memory-efficient, can produce values on-the-fly without loading entire arrays into memory. + Cons: May have slower startup times due to the need for initial setup and iterator creation. * **Array**: + Pros: Faster startup times, can be more predictable in terms of performance due to array allocation. + Cons: Requires more memory to store the entire array, which can lead to increased page faults or GC overhead. **Library Usage** There is no explicit library usage mentioned in this benchmark. However, it's worth noting that some modern JavaScript engines, like V8 (used by Chrome), have built-in support for generator functions and iterators, making them a convenient choice for generating sequences of numbers. **Special JS Features/Syntax** The only special JS feature used in this benchmark is the `yield` keyword, which allows defining generator functions. This syntax is widely supported across modern JavaScript engines and is particularly useful for creating iterative algorithms or processing large datasets without loading entire arrays into memory. **Other Alternatives** If you're interested in exploring alternative approaches to generating sequences of numbers, consider the following: * **Sliced Array**: Instead of using an array-based approach, you can use a sliced array (e.g., `Array.from(Array(100000).keys())`) to generate a sequence of numbers. * **Buffer-Based Approach**: You can also use Buffer-based approaches (e.g., `Buffer.alloc(100000)`) to generate sequences of numbers, which can be more memory-efficient for large datasets. Keep in mind that the choice of approach ultimately depends on your specific use case and performance requirements.
Related benchmarks:
Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from({ length: n }) vs new Array(n)
Array.from() vs new A
Array() vs Array.from() fill
Comments
Confirm delete:
Do you really want to delete benchmark?