Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator 2022
(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
llama3.1:latest
, generated one year ago):
Let's dive into the world of microbenchmarking and analyze what's being tested. **What is being compared?** The benchmark compares two ways to concatenate (join) arrays in JavaScript: 1. `Array.prototype.concat` method 2. Spread operator (`...`) with array literals **What are they?** 1. **Array.prototype.concat**: This is a built-in method of the Array prototype, which returns a new array that contains all the elements from one or more arrays. * Syntax: `array1.concat(array2, ...)` (concatenates multiple arrays) * Example: `[1, 2].concat([3, 4], [5])` → `[1, 2, 3, 4, 5]` 2. **Spread operator (`...`)**: A new feature introduced in ECMAScript 6 (ES6), which allows you to expand an array or object into a comma-separated list. * Syntax: `...array` (expands the array into a comma-separated list) * Example: `[1, 2].concat(...[3, 4])` → `[1, 2, 3, 4]` **Pros and cons** 1. **Array.prototype.concat**: * Pros: + Well-established method + Works with older browsers that don't support ES6 features * Cons: - Can be slower than the spread operator (as shown in the benchmark results) 2. **Spread operator (`...`)**: * Pros: + Concise and expressive syntax + Generally faster than `concat()` method (as shown in the benchmark results) * Cons: - Requires support for ES6 features, which might not be present in older browsers **Other considerations** 1. **Performance**: The spread operator is generally faster than the `concat()` method, especially when working with large arrays. 2. **Browser support**: Make sure to use feature detection or a polyfill to ensure compatibility with older browsers if you need to support them. No special JavaScript features or syntax are used in this test case, so we can just focus on understanding the basics of array concatenation in JavaScript.
Related benchmarks:
Array.prototype.concat vs Spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
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?