Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator 20
(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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The test compares two ways to create a copy of an array: the traditional `Array.prototype.slice()` method and the new ES6 spread operator (`[ ...params ]`). **Options Compared** 1. **Traditional `Array.prototype.slice()`:** * Purpose: Create a shallow copy of an array, returning a new array object with references to the original elements. * Pros: + Well-established and widely supported in older browsers. + Can be used for deep copies by calling `slice()` multiple times (e.g., `Array.prototype.slice().slice()`, etc.). * Cons: + May not be suitable for large arrays or complex data structures, as it creates a new array object with references to the original elements. 2. **New ES6 Spread Operator (`[ ...params ]`):** * Purpose: Create a shallow copy of an array using the spread operator, returning a new array object with copied elements. * Pros: + More efficient and concise than `slice()`. + Works well for large arrays or complex data structures, as it creates a new array object without references to the original elements. 3. **Other alternatives** (not tested in this benchmark): * `Array.prototype.slice.call()`: A workaround that converts an array to an array-like object using `call()` method. * `Array.prototype.map()`: Creating a new array by mapping over the original array. **Library Usage** None of the test cases use external libraries, so there are no library-specific considerations. **Special JavaScript Features or Syntax** The benchmark uses the ES6 spread operator (`[ ...params ]`), which is a relatively recent feature introduced in ECMAScript 2015. This allows for concise and expressive array creation. **Other Considerations** * The test cases use arrays with varying numbers of elements (1, 2, and 3) to ensure a good range of scenarios. * The benchmark uses Chrome 114 as the browser, which is a relatively recent version with stable performance characteristics. In summary, this benchmark tests two ways to create an array copy: the traditional `Array.prototype.slice()` method and the new ES6 spread operator. While both approaches have their pros and cons, the spread operator is generally more efficient and suitable for larger arrays or complex data structures.
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?