Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator - objects 2
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function fromNumberToObject(id = 0) { return { id, value: id + id, name: 'example', }; } var list = [...Array(100000).keys()].map(fromNumberToObject);
Tests:
Array.prototype.slice
const test = list.slice();
spread operator
const test = [...list];
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):
Let's break down the provided JSON and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark compares two approaches for creating a new copy of an array in JavaScript: 1. **`Array.prototype.slice()`**: This method returns a shallow copy of a portion of an array. 2. **Spread operator (`...`)**: Also known as the rest parameter syntax, it allows you to pass multiple arguments to a function and creates a new array from them. **Options Comparison** The benchmark compares these two approaches for creating a new copy of an array with 100,000 elements (generated using `Array(100000).keys()`). Pros and Cons: * **`Array.prototype.slice()`**: + Pros: widely supported, efficient for small arrays. + Cons: can be slow for large arrays (O(n)), creates a shallow copy, and may not preserve array modifications. * **Spread operator (`...`)** + Pros: efficient, preserves array modifications, and creates a deep copy by default. + Cons: relatively new feature, may not be supported in older browsers. **Library/Functionality** The benchmark uses the `fromNumberToObject` function to generate an array of objects. This function is not a standard JavaScript library or built-in functionality; it's a custom helper function used only for this specific benchmark. **Special JS Feature/Syntax** The benchmark uses the spread operator (`...`) feature, which was introduced in ECMAScript 2015 (ES6). It allows you to create new arrays by spreading existing arrays or other iterable objects. This feature is not supported in older browsers and may require polyfills for compatibility. **Other Alternatives** For creating a new copy of an array, you could also use: * `Array.prototype.concat()`: concatenates the elements of two or more arrays into a single array. * `Object.assign()`: copies the values of one or more source objects to a target object. * `JSON.parse(JSON.stringify(array))`: creates a deep copy of an array by serializing it as JSON and then parsing it back. However, these alternatives may have performance implications or require additional libraries/polyfills depending on your use case. The spread operator (`...`) is generally the most efficient and concise way to create a new array in modern JavaScript.
Related benchmarks:
Array.prototype.slice vs spread operator.
Array.prototype.slice vs spread operator for number only array
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?