var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].slice();
var params = [ "hello", true, 7 ]
var other = [ params ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.slice | 67915136.0 Ops/sec |
spread operator | 48713344.0 Ops/sec |
Let's break down the test case and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches to create a shallow copy of an array:
...
): This is a new syntax introduced in ECMAScript 2015 that allows creating a new array by spreading the elements of an existing array.Options being compared
The test case is comparing these two approaches for two specific scenarios:
Pros and Cons
...
):slice()
for large arrays, as it only creates a new array reference without modifying the original array. It's also more concise and readable.Library usage
In this test case, there is no explicit library being used. However, some libraries might provide additional functionality or optimizations for these operations.
Special JS features/syntax
The spread operator (...
) is a new syntax introduced in ECMAScript 2015. It's supported by most modern browsers and JavaScript engines, but may not work in older environments.
Other alternatives
Before the spread operator was introduced, developers used to use the Array.prototype.concat()
method or Array.prototype.slice().slice()
(yes, you read that right) to create shallow copies of arrays. These approaches can be slower than the spread operator and might require more code.
It's worth noting that modern JavaScript engines often optimize these operations under the hood, so the actual performance difference between these methods may vary depending on the specific implementation and use case.