Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator 73
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.slice
var params = [ "hello", true, 7 ]; var other = params.slice();
spread operator
var params = [ "hello", true, 7 ] var other = [ ...params ]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.slice
spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.slice
89184912.0 Ops/sec
spread operator
66215184.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Purpose** The benchmark compares the performance of two approaches for creating a shallow copy of an array: the `Array.prototype.slice()` method and the new ES6 spread operator (`[ ...params ]`). **Options Compared** 1. **Array.prototype.slice()**: This method creates a new array by extracting a range of elements from another array. 2. **Spread Operator (`[ ...params ]`)**: This operator creates a new array by taking all elements from the `params` array. **Pros and Cons of Each Approach** * **Array.prototype.slice()**: + Pros: - Widely supported and well-known method. - Generally faster due to its native implementation. + Cons: - Requires an extra function call (`.slice()`). - May have performance overhead depending on the browser. * **Spread Operator (`[ ...params ]`)**: + Pros: - Newer and more modern approach, which might be more efficient in some cases. - Simplifies code and reduces clutter by eliminating the need for a separate function call. + Cons: - Less widely supported (only introduced in ES6). - Might have performance overhead depending on the browser. **Library/Function Used** None, as this benchmark only compares two native JavaScript methods. **Special JS Feature/Syntax** The spread operator (`[ ...params ]`) is a new feature introduced in ECMAScript 2015 (ES6). It's designed to simplify array creation and copying by allowing you to take elements from an existing array using the `...` syntax. **Other Alternatives** In addition to these two methods, there are other ways to create a shallow copy of an array, such as: * Using the `Array.prototype.slice()` method with no arguments (which creates an empty array). * Using a library like Lodash (`_.cloneDeep()`) or a dedicated array cloning function. However, for simple cases where you only need to create a shallow copy of an array, the spread operator and `Array.prototype.slice()` methods are likely sufficient.
Related benchmarks:
Array.prototype.slice vs spread operator with length limit
Array.prototype.slice vs spread operator With slightly bigger array
Array.prototype.slice vs spread operator on a bigger array
Array.prototype.slice vs spread operator - large array 100000
Array.prototype.slice vs spread operator performance
Comments
Confirm delete:
Do you really want to delete benchmark?