Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator by sean
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator
Created:
2 years 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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.slice
233681264.0 Ops/sec
spread operator
133204992.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents two test cases for measuring the performance difference between the traditional `Array.prototype.slice()` method and the new ES6 spread operator (`...`). **What is tested:** Two options are compared: 1. **Traditional `Array.prototype.slice()`:** * This method creates a shallow copy of an array by returning a new array object with references to the same elements as the original array. 2. **New ES6 spread operator (`...`):** * This operator creates a new array by spreading the elements of the original array into a new array. **Pros and Cons:** 1. Traditional `Array.prototype.slice()`: * Pros: + Widely supported across browsers and Node.js versions. + Easy to understand and implement. * Cons: + Creates a new array object, which can lead to increased memory usage. + Can be slower due to the overhead of creating a new array object. 2. New ES6 spread operator (`...`): * Pros: + Creates a new array with minimal overhead (only a reference to the original elements). + Supports shallow copying of arrays. * Cons: + Not yet widely supported across older browsers and Node.js versions. + Can lead to issues when dealing with complex data structures or objects. **Library:** There is no specific library mentioned in the provided JSON. However, the use of ES6 syntax (`...`) indicates that the benchmark assumes a modern JavaScript environment that supports this feature. **Special JS feature or syntax:** The benchmark uses the new ES6 spread operator (`...`), which was introduced in ECMAScript 2015 (ES6). This feature allows for concise array creation and is widely supported across modern browsers and Node.js versions. **Benchmark preparation code:** The provided JSON doesn't contain any script preparation code, which means that the benchmarking environment will likely use a simple test case where an array is created with some elements, and then either `slice()` or the spread operator is used to create a new copy of the array. The exact implementation details are not shown in this JSON. **Other alternatives:** If the traditional `Array.prototype.slice()` method were not available, other alternatives for creating a shallow copy of an array might include: * Using the `Array.from()` method with the `Map` constructor (e.g., `Array.from([1, 2, 3], () => new Map([[0, 1], [1, 2]])`). Note that this approach can be slower due to the overhead of creating a new map object. * Using a library like Lodash's `cloneDeep()` function or another third-party utility for array cloning. Keep in mind that these alternatives might have different performance characteristics and may not be suitable for all use cases.
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?