Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS slice vs spread operator
(version: 0)
Compare ES6 spread operator with slice() method
Comparing performance of:
Array.prototype.slice vs spread operator
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.slice
const params = [ "hello", true, 7, { first: "first", second: "second" } ] const other = params.slice()
spread operator
const params = [ "hello", true, 7, { first: "first", second: "second" } ] 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 month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.slice
98924376.0 Ops/sec
spread operator
80422144.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Definition** The website provides a JSON definition for the benchmark, which includes: * `Name`: The name of the benchmark, which is "JS slice vs spread operator". * `Description`: A brief description of the benchmark, which compares the ES6 spread operator with the `slice()` method. * `Script Preparation Code` and `Html Preparation Code`: These fields are empty, suggesting that no specific setup code is required to run the benchmark. **Individual Test Cases** The benchmark defines two test cases: 1. **Array.prototype.slice** * The script preparation code creates an array `params` with four elements: a string, a boolean, an integer, and an object. * The test case measures the execution time of calling `slice()` on the `params` array. 2. **Spread Operator** * The script preparation code is similar to the first test case, but instead of creating an array, it uses the spread operator (`[ ...params ]`) to create a new array. **Library and Special JS Features** Both test cases use built-in JavaScript features: 1. **Array.prototype.slice**: A method on the `Array` prototype that returns a shallow copy of the array. 2. **Spread Operator (`...`)**: A syntax introduced in ES6, which allows creating a new array by iterating over the elements of an existing array. **Pros and Cons of Different Approaches** * **Slice() Method** + Pros: - Efficient for creating a shallow copy of an array. - Well-supported across browsers. + Cons: - Can be slower than the spread operator for very large arrays. - May not work as expected if the original array is modified while iterating over it. * **Spread Operator** + Pros: - Efficient and fast, even for very large arrays. - Easy to read and maintain. + Cons: - Not supported in older browsers (e.g., IE11). - May have performance implications if used with large datasets. **Other Alternatives** * **Array.from() Method**: Another way to create a new array by iterating over the elements of an existing array. While not as well-known as the spread operator, it's also efficient and widely supported. * **For...of Loop**: A syntax that allows iterating over the elements of an array without creating a new array. While less efficient than the spread operator or `Array.from()`, it can be a good choice for certain use cases. In summary, this benchmark compares the performance of two JavaScript features: the `slice()` method and the spread operator. The spread operator is generally faster and more efficient but may not work in older browsers. The benchmark provides valuable insights into the relative performance of these two approaches, which can help developers optimize their code for different use cases.
Related benchmarks:
Array.prototype.slice vs spread operator.
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 73 3
Comments
Confirm delete:
Do you really want to delete benchmark?