const elements = document.all;
const arr = Array.prototype.slice.call(elements, 100, 200);
const elements = document.all;
const amount = elements.length;
const arr = [];
for (let i = 100; i < 200; i++) arr.push(elements[i]);
const elements = document.all;
const arr = Array(100);
for (let i = 100; i < 200; i++) arr[i - 100] = elements[i];
const elements = document.all;
const arr = [];
for (let i = 100; i < 200; i++) arr[i - 100] = elements[i];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.slice | |
Array.push | |
Array[index] with known size | |
Array[index] with unknown size |
Test name | Executions per second |
---|---|
Array.slice | 3370281.2 Ops/sec |
Array.push | 217007.7 Ops/sec |
Array[index] with known size | 221830.6 Ops/sec |
Array[index] with unknown size | 218936.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark tests different ways of getting a slice or subset of an array in JavaScript. The goal is to measure the performance of each approach, likely for optimization purposes.
Options Compared
The benchmark compares four approaches:
slice()
method to create a new array with a specified start and end index.Pros and Cons
Here's a brief analysis of each approach:
Libraries and Special Features
None of the test cases use any libraries or special features in JavaScript. The focus is on comparing different array methods.
Considerations
When writing similar benchmarks, consider the following:
Alternatives
If you need to benchmark different array methods, consider alternatives like:
Keep in mind that the choice of alternative methods depends on your specific use case and performance requirements.