Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.slice() vs. Spread operator
(version: 0)
For times when a shallow copy of an Array is needed, what is the performance difference between these 2 methods.
Comparing performance of:
Array.slice() vs Spread operator
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js'></script>
Script Preparation code:
var arr = ['one', 'two', 'three', {four: 'five'}];
Tests:
Array.slice()
var myCopy = arr.slice()
Spread operator
var myCopy = [...arr]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.slice()
Spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.slice()
45668668.0 Ops/sec
Spread operator
34059944.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its results. **Benchmark Overview** The benchmark tests two ways to create a shallow copy of an array in JavaScript: the `Array.slice()` method and the spread operator (`...`). **Options Compared** There are two options being compared: 1. **Array.slice()**: This method returns a new array containing only the elements of the original array, starting from the first element. 2. **Spread Operator (...)**: This operator creates a new array by spreading out the elements of an existing array. **Pros and Cons** * **Array.slice()**: + Pros: Efficient in terms of memory usage, as it only copies references to the elements. + Cons: Can be slower than the spread operator due to the overhead of calling `slice()` and creating a new array object. * **Spread Operator (...)**: + Pros: Faster execution time, as it avoids the overhead of `slice()` and creates an array from scratch. + Cons: More memory-intensive, as it creates a new array with all elements copied. **Library Used** The benchmark uses the Lodash library, which is not strictly necessary for this specific test. However, Lodash provides utilities like `lodash.cloneDeep()` that can be used to create shallow copies of arrays. In this case, the spread operator is sufficient, and Lodash is likely included as a precautionary measure. **Special JavaScript Feature/Syntax** The benchmark uses the spread operator (`...`), which was introduced in ECMAScript 2015 (ES6). It allows you to expand an array or object into a new instance with the same elements. This feature is supported by most modern browsers and Node.js versions. **Alternative Approaches** Other ways to create shallow copies of arrays include: 1. **Array.prototype.slice.call()**: Similar to `Array.slice()`, but uses the `call()` method to call the function. 2. **Array.prototype.concat()**: Creates a new array with all elements from the original array, which can be slower than both `slice()` and the spread operator. 3. **lodash.cloneDeep()**: A more comprehensive utility for creating deep copies of arrays and objects. **Other Considerations** When choosing between these methods, consider the trade-off between memory usage and execution speed. If you need to create a shallow copy of an array frequently, `Array.slice()` might be a better choice due to its efficiency in terms of memory usage. However, if you prioritize faster execution times, the spread operator (`...`) is likely the better option. Keep in mind that these methods work differently depending on the JavaScript engine and browser used. The results from MeasureThat.net provide an accurate representation of performance differences across various browsers and devices.
Related benchmarks:
Array clone from index 1 to end: spread operator vs slice
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
JavaScript array copy via spread op vs slice
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?