Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs. spread operator
(version: 0)
Comparing performance of:
Array concat vs ES6 spread operator
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = ['a', 'b', 'c']; var arr2 = ['d', 'e', 'f'];
Tests:
Array concat
arr1.concat(arr2)
ES6 spread operator
[ ...arr1, ...arr2 ]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array concat
ES6 spread operator
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 break down the benchmark definition and test cases to understand what is being tested. **Benchmark Definition:** The benchmark measures the performance difference between two approaches: 1. `arr1.concat(arr2)` 2. `[ ...arr1, ...arr2 ]` (using the ES6 spread operator) These two approaches are used to concatenate two arrays, `arr1` and `arr2`. The main difference between them is how they handle the resulting array. **Pros and Cons of each approach:** 1. **`arr1.concat(arr2)`** * Pros: + Wide browser support (not specific to modern browsers) + Easy to understand and implement * Cons: + Creates a new, shallow copy of `arr1`, which can lead to unnecessary memory allocations. + May be slower than the spread operator due to its overhead. 2. **`[ ...arr1, ...arr2 ]` (using ES6 spread operator)** * Pros: + More efficient than `concat()` as it only creates a new array reference without creating a copy of the original arrays. + Faster execution time due to its optimized implementation in modern browsers. * Cons: + Requires modern browser support (ES6+). + May require additional setup or transpilation for older browsers. **Library:** In this benchmark, there is no explicit library mentioned. However, the use of ES6 spread operator (`[ ...arr1, ...arr2 ]`) implies that the benchmark is using a modern JavaScript engine with ES6 support. **Special JS feature/syntax:** The benchmark uses the ES6 spread operator (`[ ...arr1, ...arr2 ]`), which is a modern JavaScript syntax introduced in ECMAScript 2015. This syntax allows for concise array creation by expanding arrays or iterables. **Other alternatives:** For concatenating two arrays, other approaches include: * `arr1.push(...arr2)`: This approach modifies the original `arr1` and can be slower due to its mutability. * Array.prototype.slice().concat(arr2): This approach creates a shallow copy of `arr1` using `slice()` and then concatenates `arr2`. These alternatives may have different performance characteristics, depending on the specific use case and browser support. **Benchmark Preparation Code:** The provided preparation code sets up two arrays, `arr1` and `arr2`, with three elements each. This allows for a straightforward test of the concatenation methods. **Latest Benchmark Result:** The latest benchmark result shows that Chrome 79 on Windows Desktop achieved: * 1813151 executions per second using ES6 spread operator (`[ ...arr1, ...arr2 ]`) * 1028234 executions per second using `arr1.concat(arr2)` This suggests that the ES6 spread operator outperforms the traditional `concat()` method in this specific benchmark.
Related benchmarks:
Test concat vs spread operator
Concat vs spread operators
Concat vs Spread (Two Arrays)
Array concat() vs spread concat
Comments
Confirm delete:
Do you really want to delete benchmark?