Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.slice() vs. Spread operator (10000 items)
(version: 0)
For times when a shallow copy of an Array of size 10000 is needed, what is the performance difference between these 2 methods.
Comparing performance of:
Array.slice() vs Spread operator
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js'></script>
Script Preparation code:
var arr = new Array(10000).fill(0);
Tests:
Array.slice()
var myCopy = arr.slice()
Spread operator
var myCopy = [...arr]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.slice()
582598.9 Ops/sec
Spread operator
135642.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two methods for creating a shallow copy of an array: `Array.slice()` and the spread operator (`...`). **Options Compared** Two options are compared: 1. **`Array.slice()`**: This method creates a new array that includes only the elements from the original array, starting from the specified index (or the beginning of the array if no index is provided). 2. **Spread Operator (`...`)**: This operator creates a new array by spreading the elements of the original array. **Pros and Cons** * `Array.slice()`: + Pros: widely supported and well-documented, simple to use. + Cons: can be slower than the spread operator for large arrays due to its overhead. * Spread Operator (`...`): + Pros: concise and efficient for creating shallow copies of arrays. + Cons: not all browsers support it, and its usage can lead to security vulnerabilities if not used carefully. **Library Used** The benchmark uses the `lodash` library, which provides a utility function called `cloneDeep()` that creates a deep copy of an object. However, in this specific benchmark, only shallow copies are being compared using either `Array.slice()` or the spread operator. **Special JS Feature/Syntax** No special JavaScript features or syntax are being used in this benchmark. Both options are built-in methods and operators in JavaScript. **Other Considerations** * The benchmark is designed to measure the performance of these two methods for creating shallow copies of large arrays, such as those containing 10,000 elements. * The results may vary depending on the specific use case and the requirements of the application. For example, if the array needs to be modified after copying, using `Array.slice()` might be a better option. **Other Alternatives** If you need to create deep copies of objects or arrays, other methods like `JSON.parse(JSON.stringify(obj))` or using libraries like Lodash's `cloneDeep()` can be used. Additionally, if you're working with large datasets and performance is critical, consider using specialized data structures or algorithms optimized for your use case. In summary, this benchmark is designed to compare the performance of two simple yet effective methods for creating shallow copies of arrays in JavaScript: `Array.slice()` and the spread operator (`...`).
Related benchmarks:
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign() vs Array.slice() vs Array.slice(0)
Slice vs spread array
Array.slice() vs. Spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?