Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Array using spreading operator vs Array.slice()
(version: 0)
Comparing performance of:
Array.slice() vs spreading operator [...array]
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.slice()
var params = [ 1, 2, 3, 5, 8, 13, 21, 34 ]; var other = params.slice();
spreading operator [...array]
var params = [ 1, 2, 3, 5, 8, 13, 21, 34 ]; var other = [...params];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.slice()
spreading operator [...array]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.slice()
32610624.0 Ops/sec
spreading operator [...array]
14539355.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **What is tested?** The benchmark compares two approaches to create an array: using the `Array.slice()` method and the spreading operator (`...array`). **Options compared:** 1. **`Array.slice()`**: This method returns a shallow copy of a portion of an array. It creates a new array object with the specified elements, without modifying the original array. 2. **Spreading operator (`...array`)**: This syntax creates a new array by iterating over the elements of the original array and adding them to a new array. **Pros and Cons:** 1. **`Array.slice()`**: * Pros: + More efficient for large arrays, as it avoids creating a new array object. + Can be faster for complex array operations that require indexing or filtering. * Cons: + Creates a shallow copy of the original array, which can lead to unexpected behavior if the original array is modified later. 2. **Spreading operator (`...array`)**: * Pros: + Creates a new array with the specified elements, without modifying the original array. + Can be more readable and intuitive for simple array operations. * Cons: + May be slower than `Array.slice()` for large arrays due to the overhead of creating a new array object. + Can lead to unexpected behavior if the spread operator is used with objects or other data types. **Library and syntax:** The benchmark uses JavaScript's built-in `Array` class, which is part of the ECMAScript standard. The spreading operator (`...array`) was introduced in ECMAScript 2015 (ES6) as a way to create new arrays from existing ones. **Special JS feature or syntax:** None mentioned in this specific benchmark. **Other alternatives:** For creating an array, you can also use: * `Array.from()`: Creates a new array from an iterable object (e.g., an array, string, or set). * `concat()`: Concatenates one or more arrays into a single array. * `push()`, `unshift()`, and other methods for modifying the end of an array. However, in this specific benchmark, the only alternatives being compared are `Array.slice()` and the spreading operator (`...array`).
Related benchmarks:
Array.prototype.slice vs spread operator With slightly bigger array
Array clone from index 1 to end: spread operator vs slice
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?