Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
concat vs push vs spread performance
(version: 0)
Benchmark.js
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
Concat vs Spread operator vs Push
Created:
4 years ago
by:
Guest
Go to the latest result
Tests:
Concat
var a = [ "hello", true, 7 ]; var b = [ 1, 2 ]; var c = a.concat(b);
Spread operator
var a = [ "hello", true, 7 ]; var b = [ 1, 2 ]; var c = [ ...a, ...b ]
Push
var a = [ "hello", true, 7 ]; var b = [ 1, 2 ]; var c = a.push(...b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Concat
Spread operator
Push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Push
53.6M/s
Spread operator
29.3M/s
Concat
9.4M/s
View exact numbers
Test name
Executions per second
✓
Push
53,624,148 Ops/sec
Spread operator
29,255,606 Ops/sec
Concat
9,372,518 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The test compares the performance of three approaches: 1. `concat()` 2. Spread operator (`...`) 3. `push()` with spread syntax (`...`) All three methods are used to concatenate two arrays: `a` containing strings, booleans, and a number, and `b` containing numbers. **Library Used** There is no specific library used in this benchmark, but it's worth noting that some browsers might use polyfills or implementations of these methods if they're not available by default. **Special JavaScript Feature/Syntax** The test uses the spread operator (`...`) introduced in ES6 (ECMAScript 2015). This feature allows creating a new array by "spreading" elements from an existing array. It's also used with `push()` to spread elements into the original array. Now, let's dive into each individual test case: **Test Case 1: Concat** * Benchmark Definition: ```javascript var a = [ "hello", true, 7 ]; var b = [ 1, 2 ]; var c = a.concat(b); ``` This is the traditional way of concatenating arrays in JavaScript. The `concat()` method returns a new array and does not modify the original. Pros: * Widely supported across browsers * Simple to implement Cons: * Creates a new array, which can be memory-intensive for large datasets **Test Case 2: Spread Operator** * Benchmark Definition: ```javascript var a = [ "hello", true, 7 ]; var b = [ 1, 2 ]; var c = [...a, ...b]; ``` The spread operator (`...`) creates a new array by spreading elements from the original arrays. This method is more concise and efficient than `concat()`. Pros: * More concise than `concat()` * More efficient than `concat()` Cons: * Not supported in older browsers (pre-ES6) * May have issues with non-array values being spread **Test Case 3: Push** * Benchmark Definition: ```javascript var a = [ "hello", true, 7 ]; var b = [ 1, 2 ]; a.push(...b); ``` The `push()` method with spread syntax (`...`) modifies the original array by adding new elements at the end. This approach is more efficient than `concat()` but requires care when dealing with non-array values. Pros: * More efficient than `concat()` * Modifies the original array Cons: * May have issues with non-array values being pushed * Not as widely supported as `concat()` **Other Alternatives** If you need to concatenate arrays, other alternatives include using `Array.prototype.reduce()` or a custom implementation. However, these methods are less efficient and more complex than the three tested approaches. In summary: * For simple cases, `concat()` is a safe choice. * For performance-critical code, consider using the spread operator (`...`) for its efficiency. * When modifying the original array, use `push()` with spread syntax (`...`), but be mindful of non-array values being pushed.
Related benchmarks
concat 2 arrays: Array.prototype.concat vs spread operator
Array push vs spread when reducing over results
Comments
Confirm delete:
Do you really want to delete benchmark?