Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Array.push( spread ) vs assign Array.concat() v2.0
(version: 0)
Benchmark.js
Comparing performance of:
Array.concat() vs Array.push( spread )
Created:
6 years ago
by:
Guest
Go to the latest result
Tests:
Array.concat()
var params = [ "hello", true, 7 ]; var otherParams = [ 1, 2 ]; var other = otherParams.concat(params);
Array.push( spread )
var params = [ "hello", true, 7 ]; var otherParams = [ 1, 2 ]; var other = otherParams.push(...params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.concat()
Array.push( spread )
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
meta-externalagent/1.1 (+https://developers.facebook.com/docs/sharing/webmasters/crawler)
Browser/OS:
Other on Other
View result in a separate tab
Embed
Embed Benchmark Result
Array.push( spread )
24.3M/s
Array.concat()
5.0M/s
View exact numbers
Test name
Executions per second
✓
Array.push( spread )
24,289,648 Ops/sec
Array.concat()
4,954,282 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two methods for concatenating arrays in JavaScript: **Method 1: `Array.concat()`** * **Benchmark Definition:** This method takes an existing array (`otherParams`) and uses the `concat()` method to add elements from another array (`params`) to it. ```javascript var otherParams = [ 1, 2 ]; var params = [ "hello", true, 7 ]; var other = otherParams.concat(params); ``` * **Pros:** This is a well-established method and generally performs consistently. * **Cons:** `Array.concat()` creates a new array with the concatenated elements. This means it can have potential memory overhead, especially when dealing with large arrays. **Method 2: `Array.push()` with Spread Syntax (`...`)** * **Benchmark Definition:** This method uses the `push()` method on the first array (`otherParams`), and the spread syntax (`...params`) to add all elements from the second array into it. ```javascript var otherParams = [ 1, 2 ]; var params = [ "hello", true, 7 ]; var other = otherParams.push(...params); ``` * **Pros:** The spread syntax provides a more concise way to add elements, and it can potentially be faster than `concat()` in some cases. It modifies the original array directly, avoiding the need for creating a new array object. * **Cons:** * The `push()` method only returns the length of the modified array after adding the elements, not the modified array itself. This might require additional code if you need to access the updated array. * It directly modifies the original array, which might not be suitable for scenarios where preserving the original data is crucial. **Other Considerations:** * **Array Size:** The performance difference between these methods might become more noticeable when dealing with larger arrays. * **Specific Use Case:** Choose the method that best suits your needs. If you need to preserve the original array, `concat()` might be a better choice. If you prioritize speed and conciseness, and modifying the array in place is acceptable, `push()` with spread syntax could be more suitable. Let me know if you have any more questions!
Related benchmarks
Array.prototype.concat vs spread operator vs push with spread
Array concat vs spread operator vs push with short arrays
Comments
Confirm delete:
Do you really want to delete benchmark?