Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator (no jQuery)
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator
Created:
2 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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.concat
9893768.0 Ops/sec
spread operator
39503772.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark. **What is being tested?** The test measures the performance difference between two approaches for concatenating arrays in JavaScript: 1. The traditional `concat()` method 2. The new ES6 spread operator (`...`) **Options compared** The benchmark compares the performance of these two approaches under different conditions, but it doesn't explicitly compare other options like using `Array.prototype.push()` or `slice()`. However, we can infer that the test is primarily focused on the performance trade-offs between these two syntaxes. **Pros and Cons of each approach:** 1. **Traditional `concat()` method:** * Pros: + Widely supported and understood. + Can be used for concatenating arrays of any type, not just primitive values. * Cons: + Has a higher overhead compared to the spread operator. + May lead to unnecessary allocations of new arrays. 2. **ES6 spread operator (`...`):** * Pros: + More concise and readable syntax. + Generally faster than `concat()` for concatenating arrays of primitive values. * Cons: + Not supported in older browsers or environments. + May lead to unexpected behavior when used with non-array values. **Library usage** There is no specific library mentioned in the benchmark, but it's likely that a standard JavaScript implementation (e.g., V8) is being used. The `Array.prototype.concat()` method and the spread operator are part of the ECMAScript standard. **Special JS feature or syntax** The benchmark specifically targets the ES6 spread operator (`...`), which was introduced in ECMAScript 2015 as a new way to concatenate arrays. This syntax allows for more concise and expressive code, making it easier to work with arrays in modern JavaScript applications. **Other alternatives** If you're looking for alternative approaches to concatenating arrays, some options include: * Using `Array.prototype.push()`: This method modifies the original array by adding elements to its end. * Using `slice()`: This method creates a new array that contains all elements of the original array from a specified start index to an optional end index. In contrast to `concat()` and the spread operator, these alternatives have different performance characteristics and use cases: * `push()` can be faster for large arrays, but it modifies the original array. * `slice()` creates a new array without modifying the original, but it may lead to unnecessary allocations. Keep in mind that the choice of approach depends on your specific use case, code readability requirements, and performance constraints.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (add)
Array.prototype.concat vs spread operator (fix)
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?