Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs spread vs concat two big arrays 1000000
(version: 0)
Comparing performance of:
push vs spread vs concat
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
push
const n = 1000000; const a1 = Array(n).fill(2); const a2 = Array(n).fill(3); a1.push(...a2);
spread
const n = 1000000; const a1 = Array(n).fill(2); const a2 = Array(n).fill(3); [...a1,...a2]
concat
const n = 1000000; const a1 = Array(n).fill(2); const a2 = Array(n).fill(3); 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):
I'll break down the benchmark and its test cases to explain what's being tested. **Benchmark Overview** The benchmark compares three ways of concatenating two large arrays in JavaScript: `push`, `spread`, and `concat`. The goal is to measure which approach is the fastest for this specific use case. **Test Cases** 1. **`push`**: This test case uses the spread operator (`...`) is not present, so it's equivalent to using the `push()` method with the elements of `a2` appended to `a1`. The pros of using `push` are that it's a simple and efficient way to append elements to an array. However, its performance might be lower compared to other approaches because it involves creating a new element and appending it to the existing array. 2. **`spread`**: This test case uses the spread operator (`...`) to create a new array by copying the elements of `a1` and then spreading the elements of `a2` into the resulting array. The pros of using `spread` are that it's concise, easy to read, and can be faster than using `push()` because it avoids creating a new element. 3. **`concat`**: This test case uses the `concat()` method to concatenate the two arrays. The pros of using `concat` are that it's a well-established method for merging arrays and works in all browsers. **Library and Special Features** There is no external library being used in these test cases. However, it's worth noting that the benchmark uses JavaScript version 70 (Chrome 70), which means it's compatible with most modern JavaScript versions. **Special JS Feature or Syntax** The only special feature being tested is the spread operator (`...`) introduced in ECMAScript 2015 (ES6). This syntax allows for creating a new array by copying elements from an existing array and then spreading additional elements into the resulting array. The `push` method does not have this feature. **Other Alternatives** There are other ways to concatenate arrays in JavaScript, such as using `Array.prototype.push()` with multiple arguments or using `Array.prototype.concat()` with the second argument being an array. However, these methods are not being tested in this benchmark. Some alternative approaches that might be faster than the ones tested here include: * Using `Buffer` or `Uint8Array` to concatenate arrays, as these types have better performance characteristics for large datasets. * Using WebAssembly (WASM) or other native code implementations, which can provide significant speed improvements compared to JavaScript. Overall, this benchmark provides a simple and well-defined test case to compare the performance of different array concatenation methods in JavaScript.
Related benchmarks:
Large Array concat vs spread operator vs push
Array concat vs spread operator vs push - with large arrays5
Array concat vs spread operator vs push - with large arrays6
Array.prototype.concat vs spread operator vs push with spread
Array concat vs spread operator vs push large
Comments
Confirm delete:
Do you really want to delete benchmark?