Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs spread vs concat two array
(version: 0)
Comparing performance of:
push vs spread vs concat
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
push
const a1 = [1,2]; const a2 = [3,4] a1.push(...a2);
spread
const a1 = [1,2]; const a2 = [3,4]; [...a1,...a2]
concat
const a1 = [1,2]; const a2 = [3,4]; a1.concat(a2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
push
spread
concat
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.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is being tested?** The provided benchmark tests three different ways to concatenate (join) two arrays in JavaScript: using `push()`, `spread` operator (`...`), and `concat()` methods. The test cases are identical, with only the method used for concatenation changing. **Options compared:** 1. **`push()`**: This method appends elements to an array and returns the new length of the array. 2. **`spread` operator (`...`)**: This operator spreads the elements of one or more arrays into a new array. 3. **`concat()`**: This method concatenates two or more arrays. **Pros and Cons of each approach:** 1. **`push()`**: * Pros: Fast, simple, and widely supported. * Cons: Can be less efficient than other methods for large datasets, as it creates a new array and copies the elements. 2. **`spread` operator (`...`)**: * Pros: Efficient, modern, and widely supported (since ES6+). * Cons: May not work in older browsers or environments that don't support `...`. 3. **`concat()`**: * Pros: Sufficiently fast and well-supported. * Cons: Can be slower than other methods for large datasets due to the creation of a new array. **Libraries used (if any)** None mentioned in this specific benchmark. **Special JS features or syntax** The test cases use ES6+ syntax, specifically the spread operator (`...`). If you're not familiar with this feature, it's called "rest parameters" and allows spreading elements from one array to another. **Other alternatives** While `push()` is a simple and widely supported method, other alternatives exist, such as using `Array.prototype.push.apply()` or `Array.prototype.set()` (if the target array supports sparse arrays). However, these methods are less common in modern JavaScript development. In summary, this benchmark helps evaluate the performance of three different methods for concatenating arrays: `push()`, `spread` operator (`...`), and `concat()`. It's an interesting example of how small changes in syntax can impact performance.
Related benchmarks:
Array spread vs. push performance
array update push vs spread vs concat
Array concat vs spread operator vs push with more data
Array.prototype.concat vs spread operator vs push with spread
Array concat vs spread operator vs push with short arrays
Comments
Confirm delete:
Do you really want to delete benchmark?