Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread Operator VS Array.slice()
(version: 0)
performance test!
Comparing performance of:
Spread Operation vs Array.slice()
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Spread Operation
const words = [ 'apple', 'banana', 'orange', 'grape', 'kiwi', 'peach', 'strawberry', 'blueberry', 'pineapple', 'watermelon', 'carrot', 'broccoli', 'tomato', 'cucumber', 'potato', 'elephant', 'lion', 'giraffe', 'zebra', 'monkey' ]; const newWords = [...words];
Array.slice()
const words = [ 'apple', 'banana', 'orange', 'grape', 'kiwi', 'peach', 'strawberry', 'blueberry', 'pineapple', 'watermelon', 'carrot', 'broccoli', 'tomato', 'cucumber', 'potato', 'elephant', 'lion', 'giraffe', 'zebra', 'monkey' ]; const newWords = words.slice();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread Operation
Array.slice()
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
139385040.0 Ops/sec
Array.slice()
238615360.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Definition and Purpose** The benchmark is designed to compare the performance of two approaches: using the spread operator (`...`) and `Array.slice()` methods to create a copy of an array in JavaScript. **Options Compared** 1. **Spread Operator (`...`)**: This method creates a new array by spreading the elements of the original array into a new array. 2. **`Array.slice()`**: This method returns a shallow copy of a portion of an array, starting from the specified index and ending at the specified length. **Pros and Cons** * **Spread Operator (`...`)** + Pros: - More intuitive and readable code - Can be more efficient for small arrays due to its nature (it doesn't create a new object or array, but rather a reference to the original elements) + Cons: - May have performance issues when dealing with large arrays (as it requires creating a new array) * **`Array.slice()`** + Pros: - More efficient for large arrays as it creates a shallow copy of a portion of the original array + Cons: - Less intuitive and less readable code **Library/Function Used** None. This benchmark only uses built-in JavaScript functions. **Special JS Feature/Syntax (none)** No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** If you want to create a copy of an array, there are other alternatives: 1. **`Array.prototype.map()`**: You can use `map()` on the original array and then spread the result into a new array using the spread operator (`...`). This method creates a new array with the same length as the original array. 2. **`Array.from()`**: Similar to `map()`, you can use `from()` to create an array from an iterable object, such as an array. **Benchmark Results** The benchmark results show that: * The spread operator (`...`) is slightly faster than `Array.slice()` on this specific test case. * However, it's essential to note that the performance difference between these two methods may not be significant for all use cases and JavaScript engines. Additionally, if you're working with large arrays, `Array.slice()` might be a better choice due to its efficiency. In summary, when deciding between using the spread operator (`...`) or `Array.slice()`, consider the size of your array, readability requirements, and performance constraints.
Related benchmarks:
Array.prototype.slice vs spread operator with length limit
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
Array.prototype.slice vs spread operator performance
Comments
Confirm delete:
Do you really want to delete benchmark?