Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test push, spread, concat
(version: 0)
Comparing performance of:
push vs concat vs spread
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
push
let first = []; let second = []; for (var i; i < 10000; i++) { first.push(i); second.push(i); } first.push(...second);
concat
let first = []; let second = []; for (var i; i < 10000; i++) { first.push(i); second.push(i); } first = first.concat(second);
spread
let first = []; let second = []; for (var i; i < 10000; i++) { first.push(i); second.push(i); } first = [...first, ...second];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
push
concat
spread
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):
The provided benchmark is designed to test the performance of three different ways of merging or concatenating arrays in JavaScript: 1. **Push with spread operator (`...`)**: This approach uses the spread operator to merge two arrays by spreading the second array's elements into the first array using the `push()` method. 2. **Concatenation using `concat()`**: This approach uses the `concat()` method to merge two arrays by creating a new array and adding the elements of both arrays to it. 3. **Spread operator with array destructuring (`[...first, ...second]`)**: This approach uses the spread operator to merge two arrays by creating a new array that includes all elements from both arrays. Here are some pros and cons of each approach: * **Push with spread operator (`...`)**: + Pros: Simple, concise, and readable. It's also a relatively fast method since it avoids the creation of a new array. + Cons: Some older browsers may not support the spread operator or have performance issues with it. * **Concatenation using `concat()`**: + Pros: Widely supported across most browsers and devices. The `concat()` method is also quite efficient, especially when merging large arrays. + Cons: It can be slower than the push with spread operator approach since it involves creating a new array and copying elements from one array to another. * **Spread operator with array destructuring (`[...first, ...second]`)**: + Pros: Fast and concise. This method avoids the creation of an intermediate array, making it suitable for large arrays. + Cons: Not supported in older browsers or Internet Explorer. It also requires modern JavaScript features like rest syntax. Other considerations: * The use of `var` instead of `let` or `const` may impact performance due to variable hoisting and scope issues. * For loop vs. array methods (e.g., `for...of`) may have different performance characteristics depending on the browser and the specific use case. * The benchmark results show that the push with spread operator approach is currently the fastest, followed by concatenation using `concat()`, and then the spread operator with array destructuring. Library/Functionality usage: None of the provided benchmark definitions include any libraries or external functions. However, it's worth noting that if a library like Lodash or Ramda were used to implement these operations, it could impact performance due to the added overhead of the library. Special JavaScript features or syntax: The push with spread operator approach uses modern JavaScript features like rest syntax (`[...first, ...second]`) and the spread operator (`...`). The other two approaches do not rely on any special features.
Related benchmarks:
test concat vs spread+push
Push apply vs push spread vs concat v2
Array concat comparison between spread concat and push
spread vs spread push vs concat vs push.apply
Concat vs Push vs Spread Benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?