Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.push( spread ) vs assign Array.concat()
(version: 0)
Comparing performance of:
Array.push( spread ) vs Array.concat()
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Array.push( spread )
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].push(...params);
Array.concat()
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.push( spread )
Array.concat()
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two ways to add elements from one array to another in JavaScript: **Method 1: `Array.push()` with the spread operator (`...`)** - Code Example: `var other = [1, 2].push(...params);` - Explanation: This method uses the `push()` method on an existing array (`[1, 2]`). The spread operator (`...params`) expands the contents of the `params` array into individual elements, which are then added to the end of `other`. **Method 2: `Array.concat()`** - Code Example: `var other = [1, 2].concat(params);` - Explanation: This method uses the `concat()` method on an existing array (`[1, 2]`). It takes another array (`params`) as an argument and creates a *new* array containing all elements from both input arrays. **Pros and Cons:** * **`Array.push()` with Spread Operator:** - **Pro:** Often faster because it modifies the existing array in place. - **Con:** Can be less readable for beginners who are unfamiliar with the spread operator. * **`Array.concat()`:** - **Pro:** Creates a new array without modifying the original arrays, which can be helpful if you need to preserve the original data. - **Con:** Generally slower than `push()` because it creates a new array. **Alternatives:** * **For in loops:** You could manually iterate through the elements of `params` and push each element into `other`. This is generally less efficient than using built-in methods. **Key Considerations:** * **Performance:** If speed is critical, `Array.push()` with the spread operator is often the most performant option. * **Data Modification:** If you need to preserve the original arrays, use `Array.concat()`. Let me know if you have any other questions!
Related benchmarks:
array update push vs spread vs concat
Array concat vs spread operator vs push larger list
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?