function* rangeGenerator(end, start = 0) {
yield start
if (start === end) return
yield* rangeGenerator(end, start + 1)
}
const result = Array.from(rangeGenerator(20))
const result = []
for (let i = 0; i < 21; ++i) {
result.push(i)
}
Array.from(new Uint8Array(21).map((_, i) => i))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Generator | |
For loop | |
UIntArray |
Test name | Executions per second |
---|---|
Generator | 148275.1 Ops/sec |
For loop | 12176332.0 Ops/sec |
UIntArray | 1243712.5 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach.
Benchmark Definition JSON
The benchmark definition provides information about the test case:
Name
: "Making Array Range"Description
: null (no description is provided)Script Preparation Code
: This code defines a generator function called rangeGenerator
that yields numbers from 0 to end
, inclusive. The start
parameter has a default value of 0.Html Preparation Code
: null (no HTML preparation code is required)Individual Test Cases
Three test cases are provided, each with a different approach to create an array range:
const result = Array.from(rangeGenerator(20))
const result = []\r\nfor (let i = 0; i < 21; ++i) {\r\n result.push(i)\r\n}
Array.from(new Uint8Array(21).map((_, i) => i))
Uint8Array
data type to create an array with numbers from 0 to 20.Options Compared
The three test cases compare different approaches to creating an array range:
Uint8Array
data type, which is a typed array that can store unsigned 8-bit integers. This approach may offer performance benefits due to its optimized memory layout and caching.Pros and Cons
Here are some pros and cons for each approach:
Library and Special JS Feature
No libraries are explicitly mentioned in the benchmark definition or test cases. However, the Uint8Array
data type is used, which is a part of the JavaScript Standard Library.
There are no special JavaScript features or syntax explicitly mentioned in the provided code.