Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs arr
(version: 0)
aaa
Comparing performance of:
spread vs arr
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
spread
const spread = [1, 2, ...['a', true, 2]]
arr
const spread = [1, 2].push('a', true, 2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
arr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
54934952.0 Ops/sec
arr
74383480.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two ways of creating arrays in JavaScript: using the spread operator (`...`) and using the `push()` method. **Options Compared:** * **`spread`**: This approach uses the spread operator to combine multiple arrays into a single new array. In this case, it's taking an array `[1, 2]` and adding elements from another array `['a', true, 2]`. * **`arr`**: This approach uses the `push()` method to add elements one by one to an existing array. It starts with `[1, 2]` and then adds `'a'`, `true`, and `2` sequentially. **Pros and Cons:** * **Spread Operator (`...`)**: * **Pros:** More concise and readable syntax, can be used to combine multiple arrays or iterables efficiently. * **Cons:** Can potentially create more garbage (temporary arrays) depending on the structure of the data being spread. Performance-wise it might not always be the fastest option, as it involves a deeper copy operation. * **`push()` Method**: * **Pros:** More traditional approach, can modify an existing array in place, potentially slightly faster for adding elements one by one. * **Cons:** Less readable syntax when adding multiple elements, requires iterating through the elements individually. **Other Considerations:** The choice between these methods often comes down to personal preference and the specific use case. For simple cases like this, the performance difference might be negligible. However, in more complex scenarios with large arrays or frequent modifications, optimizing for memory usage or execution speed could become crucial. **Alternatives:** * **Array Concatenation (`+`)**: ```javascript const spread = [1, 2].concat(['a', true, 2]); // Similar to using the spread operator ``` While readable, it can be less efficient for large arrays due to potential array copying. * **`unshift()` Method**: If you want to add elements to the *beginning* of an array instead of the end, `unshift()` is a suitable alternative.
Related benchmarks:
Array spread vs. push performance
Array .push() vs .unshift() vs spread
Push vs Spread JavaScript
Array .push() vs spread operator
Array.prototype.slice vs spread operator performance
Comments
Confirm delete:
Do you really want to delete benchmark?