Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator1223
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [3, 4, 1, 3, 5, 6, 3]; var other = params.concat().sort((a, b) => a - b);
spread operator
var params = [3, 4, 1, 3, 5, 6, 3]; var other = [...params].sort((a, b) => a - b);
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 tests the performance of two methods for creating a sorted copy of an array in JavaScript: * **`Array.prototype.concat()`**: This method creates a new array by copying elements from one or more arrays. In this test case, it's used to create a new array containing all elements from the original `params` array and then sort it using the `sort()` method. * **Spread Operator (`...`)**: The spread operator expands an iterable (like an array) into its individual elements. In this test, it's used to create a new array containing all elements of `params`, which is then sorted using the `sort()` method. **Pros and Cons:** | Method | Pros | Cons | |---|---|---| | `concat()` | Widely supported | Can be slower than the spread operator, especially for large arrays. | | Spread Operator | Generally faster for creating new arrays, concise syntax | Requires ES6 (ECMAScript 2015) support. | **Alternatives:** * **`slice()`**: Creates a shallow copy of a portion of an array. You could combine it with `sort()` for similar functionality. * **`.map()` and `.sort()`**: Map creates a new array by applying a function to each element, and sort sorts the newly created array. This offers more flexibility if you need to modify elements during the copying process. **Considerations:** * **Array Size**: The performance difference between `concat()` and the spread operator might become more significant with larger arrays. * **Browser Support**: Ensure that your target audience has access to ES6 or consider polyfills for the spread operator if supporting older browsers. Let me know if you have any more questions about this benchmark!
Related benchmarks:
Array.prototype.concat vs Spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator on small array
Array.prototype.concat vs spread operator 12
Comments
Confirm delete:
Do you really want to delete benchmark?