Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator [FIXED]
(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
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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.slice
59368832.0 Ops/sec
spread operator
43083576.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to create a shallow copy of an array: the traditional `Array.prototype.slice()` method and the new ES6 spread operator (`[...array]`). **Options Compared** There are two options being compared: 1. **Traditional `Array.prototype.slice()`:** This method returns a new, shallow-copied version of the original array. 2. **New ES6 spread operator (`[...array]`):** This method creates a new array by spreading the elements of the original array. **Pros and Cons** * **Traditional `Array.prototype.slice()`:** + Pros: - Widely supported across different browsers and versions. - Simple to implement. + Cons: - Can be slower than the spread operator due to the overhead of creating a new object and iterating over the original array. * **New ES6 spread operator (`[...array]`):** + Pros: - More concise and readable. - Can be faster than traditional `slice()` since it avoids the overhead of creating a new object and iterating over the original array. + Cons: - Not supported in older browsers or versions (e.g., Internet Explorer 11). - May have performance issues with very large arrays due to memory allocation. **Library and Purpose** There is no external library used in this benchmark. The spread operator is a built-in JavaScript feature introduced in ES6. **Special JS Feature or Syntax** The spread operator (`[...array]`) is a new syntax introduced in ES6, which allows you to create a new array by spreading the elements of an existing array. This feature is widely supported across modern browsers and versions. **Other Alternatives** If you need to support older browsers or versions, you can use other methods to create a shallow copy of an array, such as: * `Array.prototype.slice.call()`: This method calls the `slice()` method on an array-like object and returns a new array. * `Array.prototype.slice.apply()`: This method applies the `slice()` method to an array-like object and returns a new array. However, these methods may not be as concise or readable as the spread operator. **Benchmark Preparation Code** The benchmark preparation code is empty (`"Script Preparation Code": null`, `"Html Preparation Code": null`), which means that no additional setup or configuration is required for this benchmark. The focus is solely on comparing the performance of the two array copying methods.
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?