Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator test 34234
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params);
spread operator
var params = [ "hello", true, 7 ] var other = [ 1, 2, ...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 is part of JavaScript's built-in array functionality. It takes one or more arrays as arguments and returns a new array containing all the elements from the original arrays. * **Pros:** Widely supported, well-documented. * **Cons:** Can be slightly slower than the spread operator in modern JavaScript engines. * **Spread Operator (`...`)**: Introduced in ES6 (ECMAScript 2015), this operator expands an iterable object into its individual elements. When used with arrays, it effectively copies all elements into a new array or another context. * **Pros:** More concise and readable syntax, often faster than `concat()`. * **Cons:** Introduced relatively recently, might not be supported in older browsers. **The benchmark tests these approaches by:** 1. Defining two arrays: `[ 1, 2 ]` and `[ "hello", true, 7 ]`. 2. Creating a new array using `concat()` which combines the first array with the second. 3. Creating a new array using the spread operator which also combines the arrays. **The results show:** * The spread operator (`...`) is significantly faster in this particular case (45 million executions per second) compared to `concat()` (9 million executions per second). This difference highlights the potential performance advantage of modern syntax. **Alternatives:** While `concat()` and the spread operator are common, other options exist for combining arrays: * **`Array.from()`**: Creates a new array from an iterable object. Can be useful when working with non-array iterables. * **Manual Copying**: You could manually iterate through arrays and build a new one. This is generally less efficient than built-in methods. Let me know if you'd like to dive deeper into any specific aspect or have more questions about JavaScript performance!
Related benchmarks:
Array.prototype.concat vs Spread operator
Array.prototype.concat vs Spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
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?