Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Large Array: concat vs spread vs push
(version: 0)
Compares the three mechanisms for combining arrays with 1k arrays
Comparing performance of:
Concat vs Spread vs Push
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = Array.from(Array(1000).keys());
Tests:
Concat
var arr = []; arr = arr.concat(arr1);
Spread
var arr = []; arr = [...arr, ...arr1];
Push
var arr = []; arr.push(...arr1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Concat
Spread
Push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Concat
5518470.5 Ops/sec
Spread
334098.1 Ops/sec
Push
631095.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a benchmark test for comparing the performance of three mechanisms for combining arrays: `concat`, `spread`, and `push`. The test aims to determine which method is the most efficient in JavaScript. **Benchmark Test** The test creates an array `arr1` with 1000 elements using `Array.from(Array(1000).keys())`. Then, it sets up three individual test cases: 1. **Concat**: The test case creates an empty array `arr`, and then concatenates `arr1` to `arr` using the `concat()` method. 2. **Spread**: The test case creates an empty array `arr`, and then uses the spread operator (`...`) to add all elements of `arr1` to `arr`. 3. **Push**: The test case creates an empty array `arr`, and then uses the `push()` method to add all elements of `arr1` to `arr`. **Options Compared** The three options are compared in terms of their performance, measured by the number of executions per second. **Pros and Cons of Each Approach** * **Concat**: This approach creates a new array by copying elements from one array to another. It can be inefficient if the arrays are large, as it requires additional memory allocations. * **Spread**: This approach uses the spread operator to add elements from one array to another. It is generally more efficient than `concat()` because it avoids creating an intermediate array and only adds references to the original elements. * **Push**: This approach modifies the existing array by adding new elements to the end of the array. It can be slower than `concat()` or `spread` because it involves updating the internal array buffer. **Library Used** The test does not use any libraries specifically, but it uses built-in JavaScript features like arrays and the spread operator. **Special JS Features/ Syntax** None are mentioned in this specific benchmark test. **Other Alternatives** If you want to explore other alternatives for combining arrays, you can consider: * Using `Array.prototype.set()`: This method is not as widely supported as `push()` or `concat()`, but it allows adding elements to an array without having to specify the index. * Using `Array.prototype.splice()`: This method allows removing and replacing elements in an array. While not directly related to combining arrays, it can be used creatively for array manipulation. Keep in mind that these alternatives may have different performance characteristics or syntax nuances compared to `concat`, `spread`, and `push`.
Related benchmarks:
Array spread vs. push performance
Array push vs spread vs concat
.concat vs. spread
Array concat vs spread operator vs push large
Comments
Confirm delete:
Do you really want to delete benchmark?