Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator 73 3
(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
Script Preparation code:
params = [ "hello", true, 7 ];
Tests:
Array.prototype.slice
const other = params.slice();
spread operator
const 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
8155867.5 Ops/sec
spread operator
7928690.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and its options to help you understand what's being tested. **Benchmark Definition** The benchmark is designed to compare two methods for creating a shallow copy of an array: `Array.prototype.slice()` and the new ES6 spread operator (`[ ...params ]`). **Script Preparation Code** ```javascript params = [ "hello", true, 7 ]; ``` This code creates an array `params` with three elements. **Html Preparation Code (null)** There is no HTML preparation code provided, which means this benchmark only tests the JavaScript engine's performance. **Individual Test Cases** The test cases are: 1. **Array.prototype.slice()** ```javascript const other = params.slice(); ``` This line of code creates a new array `other` by calling the `slice()` method on the original `params` array, starting from the beginning and ending at the end. 2. **Spread Operator (`[ ...params ]`)** ```javascript const other = [ ...params ]; ``` This line of code creates a new array `other` using the spread operator, which creates a shallow copy of the `params` array by copying each element into a new array. **Options Compared** The benchmark is comparing two options for creating an array copy: 1. **Array.prototype.slice()**: This method returns a new array containing the elements from the original array, starting from the first element (index 0) to the last element. 2. **Spread Operator (`[ ...params ]`)**: This operator creates a shallow copy of the original array by creating a new array and copying each element from the original array into it. **Pros and Cons** Here are some pros and cons of each approach: * **Array.prototype.slice()**: + Pros: It's a well-established method that has been part of JavaScript for a long time. It's also relatively efficient. + Cons: It creates a new array object, which can be memory-intensive if the original array is large. * **Spread Operator (`[ ...params ]`)**: + Pros: It's concise and easy to read. It also creates a shallow copy of the original array without the need for an extra function call. + Cons: Some older JavaScript engines or browsers might not support this operator, which could lead to performance issues. **Library (if applicable) and Special JS Features** There are no libraries used in these benchmarked test cases. However, it's worth noting that the spread operator was introduced as a part of the ECMAScript 2015 (ES6) standard. If the benchmark tested other JavaScript features or syntax not mentioned here, it would be helpful to know what those features are, but unfortunately, there is no information provided in this case.
Related benchmarks:
Array.prototype.slice vs spread operator 1
arr.slice() vs spread operator
Array.prototype.slice vs spread operator With slightly bigger array
Which is faster Array.prototype.slice vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?