var array = Array.from({ length: 100 }).map((val, i) => i);
var newArray = array.toSpliced(0, 0, 99);
var newArray = [99, array]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
toSpliced | |
Spread |
Test name | Executions per second |
---|---|
toSpliced | 34988224.0 Ops/sec |
Spread | 5152974.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
What is being tested?
The benchmark measures the performance difference between two approaches for creating a new array by removing elements from an existing array: Array.prototype.toSpliced()
(if available) and the spread operator (...
).
Options compared
There are two options being compared:
toSpliced()
: This is a method in the Array prototype that creates a new array with the specified range of elements removed from the original array. It's called toSpliced()
instead of splice()
, which is the more traditional way to remove elements from an array....
): This is a feature introduced in ECMAScript 2015 (ES6) that allows creating a new array by spreading the elements of an existing array.Pros and cons
Here are some pros and cons of each approach:
toSpliced()
:...
):toSpliced()
for large arrays, since it creates a new array by copying all elements.Other considerations
toSpliced()
and the spread operator. However, if you're targeting older browsers or versions of JavaScript, you may need to use one approach over the other.Library usage
There doesn't appear to be any libraries used in this benchmark.
Special JS feature or syntax
The benchmark uses a modern JavaScript feature: the spread operator (...
). This is introduced in ECMAScript 2015 (ES6) and is widely supported in modern browsers and JavaScript engines.