var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const arr1 = [ , arr ];
const arr1 = arr.slice(1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread operator | |
slice |
Test name | Executions per second |
---|---|
spread operator | 5929999.0 Ops/sec |
slice | 6548163.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is testing two approaches to create a clone of an array from index 1 to the end: using the spread operator (...
) versus using the slice()
method.
Options Compared
The two options being compared are:
...
): This syntax creates a new array by spreading the elements of the original array, starting from the second element (index 1).arr.slice(1)
): This method returns a shallow copy of a portion of an array, specifically the slice from index 1 to the end.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
...
):arr.slice(1)
):slice()
methodOther Considerations
The benchmark also considers other factors, such as:
Library and Special JS Features
In this benchmark, there is no library being used. However, it's worth noting that the spread operator was introduced in ECMAScript 2015 (ES6), so browsers may not support it if they're using an older JavaScript engine.
Other Alternatives
If you wanted to create a clone of an array from index 1 to the end, other alternatives could include:
Array.prototype.slice.call(arr, 1)
or Array.prototype.slice.apply(arr, [1])
(older syntax for creating an array copy)Array.from(arr, (_, i) => arr[i + 1])
, which is another spread-based approachHowever, these alternatives may not be as concise or readable as the spread operator or slice method.
I hope this explanation helps you understand what's being tested in this benchmark!