Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
(version: 0)
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
Comparing performance of:
Spread Operation vs Array.prototype.slice() vs Array.prototype.slice(0)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var randomNumbers = []; for (let i = 0; i < 2000; i++) { randomNumbers.push(Math.random()); }
Tests:
Spread Operation
const copy = [...randomNumbers];
Array.prototype.slice()
const copy = randomNumbers.slice();
Array.prototype.slice(0)
const copy = randomNumbers.slice(0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread Operation
Array.prototype.slice()
Array.prototype.slice(0)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread Operation
1873710.0 Ops/sec
Array.prototype.slice()
1825417.0 Ops/sec
Array.prototype.slice(0)
1704650.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is tested on the provided JSON?** The benchmark measures the performance of three different approaches to create a copy of an array in JavaScript: 1. **Spread Operator (`const copy = [...randomNumbers];`)**: This approach creates a new array by spreading the elements of `randomNumbers`. 2. **Array.prototype.slice()` with no arguments (`const copy = randomNumbers.slice();`)**: This approach returns a shallow copy of `randomNumbers`, creating a new array. 3. **Array.prototype.slice()` with an argument (0) (`const copy = randomNumbers.slice(0);`)**: This approach creates a new array containing only the first element of each sub-array in `randomNumbers`. **Options compared** The benchmark compares the performance of these three approaches: * Spread Operator * Array.prototype.slice() without arguments * Array.prototype.slice() with an argument (0) **Pros and Cons of each approach:** 1. **Spread Operator (`const copy = [...randomNumbers];`)**: * Pros: Creates a new array, can be more efficient than other approaches for large arrays. * Cons: May not be as readable or maintainable as other options. 2. **Array.prototype.slice()` without arguments (`const copy = randomNumbers.slice();`)**: * Pros: Easy to read and understand, creates a shallow copy of the array. * Cons: May not create a new array for large arrays, can lead to unexpected behavior if used incorrectly. 3. **Array.prototype.slice()` with an argument (0) (`const copy = randomNumbers.slice(0);`)**: * Pros: Creates a new array containing only the first element of each sub-array in `randomNumbers`, can be more efficient than other approaches for large arrays. * Cons: May not create a new array, can lead to unexpected behavior if used incorrectly. **Library and its purpose** There is no external library used in this benchmark. However, JavaScript arrays are based on the ECMAScript specification, which defines the behavior of arrays and their methods, including `slice()`. **Special JS feature or syntax** There is no special JS feature or syntax mentioned in this benchmark. **Other alternatives** If you're looking for alternative approaches to create a copy of an array in JavaScript, some other options include: * Using the `Array.prototype.concat()` method: `const copy = randomNumbers.concat();` * Using the `JSON.parse(JSON.stringify(randomNumbers))` trick: `const copy = JSON.parse(JSON.stringify(randomNumbers));` * Using a library like Lodash's `cloneDeep` function Note that each of these alternatives has its own trade-offs and performance characteristics, which may be worth considering depending on your specific use case.
Related benchmarks:
Array.prototype.slice vs spread operator on a bigger array
Array slice() vs slice(0) vs spread operatorr
Array.prototype.slice vs spread operator - large array 100000
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?