Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator 2D
(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 = [ [1, 2], [3, 4], [5, 6], [7, 8], [9, 10] ]; var ans = []; for (let i = 0; i < params.length; i++) { ans.push(params[i].slice()); }
spread operator
var params = [ [1, 2], [3, 4], [5, 6], [7, 8], [9, 10] ]; var ans = []; for (let i = 0; i < params.length; i++) { ans.push([...params[i]]); }
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.slice
14503165.0 Ops/sec
spread operator
11133719.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 designed to compare two approaches for creating a new array with data from an existing 2D array: 1. **Array.prototype.slice**: The traditional method of using `slice()` to extract a subset of elements from an array. 2. **Spread Operator (`...`)**: The new ES6 spread operator, introduced in ECMAScript 2015, which allows creating a new array by spreading the elements of another array. **Options Compared** The benchmark is comparing two options: 1. **Array.prototype.slice**: This method creates a shallow copy of the specified range of elements from an array. 2. **Spread Operator (`...`)**: This operator creates a new array by spreading the elements of another array. **Pros and Cons** * **Array.prototype.slice**: + Pros: - Well-established and widely supported - Generally faster for small to medium-sized arrays + Cons: - Can be slower for large arrays due to the overhead of creating a new array - May not work as expected with sparse arrays or arrays with non-contiguous indices * **Spread Operator (`...`)**: + Pros: - More concise and readable than `slice()` - Generally faster for large arrays, since it avoids creating an intermediate array + Cons: - May not work as expected with certain types of data (e.g., non-array values) - Requires support for ES6 spread operator syntax **Library** There is no external library used in this benchmark. Both `Array.prototype.slice` and the spread operator are built-in JavaScript features. **Special JS Feature or Syntax** The spread operator (`...`) is a new feature introduced in ECMAScript 2015. It's not yet widely supported in older browsers, so it's likely that Chrome 128 is one of the few browsers that supports it. In summary, this benchmark is designed to compare the performance of two approaches for creating a new array with data from an existing 2D array: `Array.prototype.slice` and the spread operator (`...`). The benchmark is interested in understanding which approach is faster and more efficient for large datasets. **Other Alternatives** If you want to create a new array by spreading elements from another array, you can use other methods, such as: * Using `Array.from()` with an iterable: `Array.from([1, 2], () => [3, 4])` * Using `map()` and `concat()`: `[].concat(...[1, 2].map(() => [3, 4]))` However, these methods may not be as efficient as the spread operator for large datasets.
Related benchmarks:
Array.prototype.slice vs spread operator.
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
Comments
Confirm delete:
Do you really want to delete benchmark?