Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator 2
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var params2 = [ 1, 2 ] var other = params2.concat(params);
spread operator
var params = [ "hello", true, 7 ] var params2 = [ 1, 2 ] var other = [ ...params2, ...params ]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
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 combine arrays in JavaScript: the traditional `Array.prototype.concat()` method and the newer spread operator (`...`). **Here's a breakdown:** * **`Array.prototype.concat()`:** This method takes one or more arrays as arguments and returns a new array containing all the elements from the original arrays. * **Pros:** Widely supported, well-established. * **Cons:** Can be slightly slower than the spread operator in modern JavaScript engines due to its need for internal memory allocations. * **Spread Operator (`...`)**: Introduced in ES6, this operator expands the elements of an array into individual items when used within another array literal. * **Pros:** More concise syntax, often performs better in performance tests because it's more directly integrated with how JavaScript handles arrays internally. * **Cons:** Requires understanding of ES6 syntax, might not be as widely supported in older browsers (although widespread browser support is good). **In the provided test case:** * Both methods combine the array `[1, 2]` (`params2`) with the array `["hello", true, 7]` (`params`). * The results show that the spread operator (`...`) performs slightly better in this particular scenario. **Alternatives:** While not directly compared here, other options exist: * **`push()` method:** You can use the `push()` method to add elements one by one to an existing array. This can be less efficient for large arrays. * **Looping and manual concatenation:** In older JavaScript versions before ES6, you might manually iterate through arrays using a loop and concatenate their values into a new array. This is generally inefficient and less readable than the other methods. Let me know if you'd like to explore any of these options in more detail or have any further questions!
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator real
Array.prototype.concat vs spread operator on large array
Array.prototype.concat vs spread operator on small array
Comments
Confirm delete:
Do you really want to delete benchmark?