Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
rand012983903218
(version: 0)
rand
Comparing performance of:
slice vs spread
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var randObj = { "id": 1, "something": 2, "nested": { "ab": "c", "d": "e" }, "ok": false } var arr = [] for (i = 0; i < 1000; i++) { arr.push(randObj) }
Tests:
slice
let newArr = arr.slice()
spread
let newArr = [...arr]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
slice
spread
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; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
204753.7 Ops/sec
spread
78664.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided benchmark. The benchmark measures the performance of two different ways to create a new array from an existing one: 1. `arr.slice()`: This method returns a shallow copy of a portion of an array, but it doesn't modify the original array. 2. `arr[:]` (also known as the spread operator): This method creates a new array by copying all elements from the original array. Now, let's discuss the pros and cons of each approach: **`arr.slice()`** Pros: * Creates a shallow copy of the original array without modifying it. * Can be faster for large arrays since it doesn't require creating a new array object with multiple properties (like the spread operator does). Cons: * Returns a new array, which can lead to memory allocation and garbage collection overhead. * May not be as efficient for very large datasets since it requires allocating new memory. **`arr[:]` (spread operator)** Pros: * Creates a new array by copying all elements from the original array, just like the spread operator in other languages. * Can be faster for small to medium-sized arrays since it's often optimized for this case. Cons: * Returns a new array, which can lead to memory allocation and garbage collection overhead. * May not be as efficient for very large datasets since it requires allocating new memory. The choice between these two approaches depends on the specific use case. If you need to modify the original array or work with small arrays, `arr.slice()` might be a better choice. However, if you're working with large datasets and want to create a new array without modifying the original one, the spread operator (`arr[:]`) might be faster. In this benchmark, we can see that Firefox 129 performs significantly better for both methods, but the gap between the two is smaller than expected. As for other alternatives, there are a few more ways to create a new array from an existing one: * `arr.concat()`: This method creates a new array by concatenating all elements from the original array. It's similar to `slice()` in that it returns a shallow copy of the original array. * `Array.prototype.map(arr)` or `arr.map()`: These methods create a new array by applying a function to each element in the original array. They can be slower than `slice()` and the spread operator since they require executing a function for each element. However, these alternatives might not be as efficient as the two main approaches being tested here. As for libraries, there are no external libraries used in this benchmark. The test code only uses built-in JavaScript methods like `slice()`, the spread operator (`arr[:]`), and the `Array.prototype.map()` method. Finally, there's a special JS feature at play here: the spread operator (also known as array destructuring or rest parameters). It was introduced in ECMAScript 2015 (ES6) and allows you to create new arrays by copying elements from an existing one. In this benchmark, the spread operator is used to create a new array by spreading all elements from `arr`. That's it! I hope this explanation helps clarify what's being tested in this benchmark.
Related benchmarks:
Get last array element
array creation shenanigans
teststest
teststest1
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?