Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs Spread (2)
(version: 0)
Comparing performance of:
Array.prototype.slice vs Spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.slice
var fooSet = []; for(var i=0;i<100;i++) { fooSet.push(i); } var other = fooSet.slice(0);
Spread
var fooSet = []; for(var i=0;i<100;i++) { fooSet.push(i); } var other = [...fooSet];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.slice
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Browser/OS:
Safari 18 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
2694673.0 Ops/sec
Spread
2532534.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Overview** The test case is comparing two approaches to create a shallow copy of an array: 1. `Array.prototype.slice()` 2. The spread operator (`...`) These two methods are often used interchangeably, but they have slightly different behavior and performance characteristics. **Options Compared** * **Array.prototype.slice()**: This method returns a new array object containing the elements from the original array, starting from the specified index (in this case, `0`). The returned array is a shallow copy of the original array. * **Spread operator (`...`)**: This method creates a new array by spreading the elements of an iterable (like an array) into a new array. **Pros and Cons** * **Array.prototype.slice()**: + Pros: - Widely supported and well-documented - Can be used to create a shallow copy of an array + Cons: - Can be slower than the spread operator for large arrays, since it creates a new array object and copies the elements one by one. * **Spread operator (`...`)**: + Pros: - Faster than `Array.prototype.slice()` for large arrays, since it uses a more efficient copying mechanism + Cons: - Requires support for modern JavaScript syntax (ES6+) and may not work in older browsers or environments. **Library Used** None in this case. The benchmark only uses built-in JavaScript features. **Special JS Feature or Syntax** The spread operator (`...`) is a new syntax introduced in ECMAScript 2015 (ES6+). It's used to create a new array by spreading the elements of an iterable into a new array. **Other Alternatives** * **Array.prototype.concat()**: This method returns a new array containing all elements from both arrays. While it can be used to create a shallow copy, it's not as efficient as the spread operator for large arrays. * **Array.from()**: This method creates a new array from an iterable (like an array or string). It's not directly comparable to the spread operator in terms of performance. In summary, this benchmark is testing the performance difference between two common ways to create a shallow copy of an array in JavaScript: `Array.prototype.slice()` and the spread operator (`...`). The results can help developers understand which approach to use depending on their specific use case and performance requirements.
Related benchmarks:
Array.prototype.slice vs spread operator.
Array.prototype.slice vs spread operator with length limit
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?