Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs spread operator
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator
Created:
6 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down what's being tested here. **Benchmark Description** The benchmark is comparing two ways to create a copy of an array in JavaScript: using the `slice()` method and using the spread operator (`...`). **Test Cases** 1. **Array.prototype.slice**: This test case uses the traditional `slice()` method to create a new array from an existing one. * Code: `var params = [ "hello", true, 7 ]; var other = params.slice();` * Library/Feature: None * Description: Simply calls the `slice()` method on the original array `params` to create a new array `other`. 2. **Spread operator**: This test case uses the spread operator (`...`) to create a new array from an existing one. * Code: `var params = [ "hello", true, 7 ]; var other = [ ...params ];` * Library/Feature: ES6 Spread Operator (introduced in ECMAScript 2015) * Description: Uses the spread operator to "spread" the elements of the original array `params` into a new array `other`. **Benchmark Results** The latest benchmark results show that on a Chrome 84 browser on Windows, running the `Array.prototype.slice` test case executes at approximately 121.8 million times per second, while the `Spread operator` test case executes at approximately 76.2 million times per second. **Pros and Cons of each approach** * **slice() method**: Pros: widely supported by older browsers, simple to use; Cons: might be slower than spread operator in modern browsers. * **Spread operator**: Pros: concise syntax, potentially faster than slice() in modern browsers; Cons: introduced in ES6, might not work in older browsers or environments without transpilation. **Other Alternatives** For creating a copy of an array in JavaScript, other alternatives include: * Using `concat()` method (not tested here) * Using a loop to manually create a new array * Using a library like Lodash's `clone()` function (if needed) Keep in mind that the actual performance difference between these approaches might vary depending on your specific use case and environment.
Related benchmarks:
Array.prototype.slice vs spread operator 123
Array.prototype.slice vs spread operator with length limit
arr.slice() vs spread operator
Array.prototype.slice vs spread operator With slightly bigger array
Array.prototype.slice vs spread operator - large array 100000
Comments
Confirm delete:
Do you really want to delete benchmark?