var params = [ "hello", true, 7 ];
var other = params.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 | 51520644.0 Ops/sec |
spread operator | 37877680.0 Ops/sec |
Let's break down the provided JSON benchmark definition and test cases.
What is being tested?
The benchmark compares two ways to create a shallow copy of an array: the traditional Array.prototype.slice()
method and the new ES6 spread operator ([...params]
).
Options compared:
Array.prototype.slice()
[...params]
slice()
for large arrays, since it avoids creating a new array objectOther considerations:
Library usage:
None of the test cases rely on external libraries.
Special JS feature/syntax:
The spread operator ([...params]
) is a new JavaScript syntax introduced in ES6. It's a concise way to create an array from an iterable (like an array, string, or object).
Benchmark preparation code and HTML preparation code:
The JSON benchmark definition doesn't provide any specific preparation code for the test cases. This suggests that the test cases are self-contained and can be run directly in the browser.
Other alternatives:
If you want to compare other methods for creating shallow copies of arrays, you could consider adding additional test cases for:
Array.prototype.concat()
Object.assign()
or Array.prototype.slice.call()
_.slice()
)