Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Making Array Range
(version: 0)
Comparing performance of:
Generator vs For loop vs UIntArray
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function* rangeGenerator(end, start = 0) { yield start if (start === end) return yield* rangeGenerator(end, start + 1) }
Tests:
Generator
const result = Array.from(rangeGenerator(20))
For loop
const result = [] for (let i = 0; i < 21; ++i) { result.push(i) }
UIntArray
Array.from(new Uint8Array(21).map((_, i) => i))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Generator
For loop
UIntArray
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Generator
654890.2 Ops/sec
For loop
34640328.0 Ops/sec
UIntArray
2776886.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Generator** * Benchmark Definition: `const result = Array.from(rangeGenerator(20))` * Description: This test case uses the generator function defined in the script preparation code. 2. **For Loop** * Benchmark Definition: `const result = []\r\nfor (let i = 0; i < 21; ++i) {\r\n result.push(i)\r\n}` * Description: This test case uses a traditional for loop to create an array with numbers from 0 to 20. 3. **UIntArray** * Benchmark Definition: `Array.from(new Uint8Array(21).map((_, i) => i))` * Description: This test case uses the `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: 1. **Generator**: Uses a generator function to yield numbers, which can be more memory-efficient and potentially faster than traditional for loops. 2. **For Loop**: Uses a traditional for loop to iterate over the numbers, which is more straightforward but may incur overhead due to loop control statements. 3. **UIntArray**: Utilizes a `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: 1. **Generator**: * Pros: Can be more memory-efficient, potentially faster execution. * Cons: May require more complex code, less intuitive for beginners. 2. **For Loop**: * Pros: Easy to understand, straightforward implementation. * Cons: May incur overhead due to loop control statements, slower execution. 3. **UIntArray**: * Pros: Optimized memory layout and caching can offer performance benefits. * Cons: Requires a typed array, which may have additional overhead. **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.
Related benchmarks:
array.from vs array slice
slice vs splice
Array.slice(-1) vs Array.At(-1) Teste Pro Eduzeira
testando 123 teste
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?