Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array concat vs spread create new array
(version: 0)
Comparing performance of:
spread vs concat
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
spread
var a = [1,2,3]; var b = [4,5,6]; var c = [...a, ...b]
concat
var a = [1,2,3]; var b = [4,5,6]; var c = a.concat(b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
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 break down what's being tested in this benchmark. **Benchmark Definition** The benchmark is testing the performance difference between two approaches: concatenating arrays using the `concat()` method and spreading an array using the spread operator (`...`). **Options Compared** There are two options compared: 1. **Concatenation**: This approach uses the `concat()` method to concatenate two arrays, `[a]` and `[b]`. The resulting array is stored in variable `c`. 2. **Spreading**: This approach uses the spread operator (`...`) to create a new array by spreading the elements of both input arrays, `[a]` and `[b]`. The resulting array is also stored in variable `c`. **Pros and Cons** * **Concatenation**: + Pros: Simple and easy to understand. It's been around for a long time and has good browser support. + Cons: Can be slower than spreading, especially when dealing with large arrays. * **Spreading**: + Pros: Generally faster than concatenation, as it avoids creating an intermediate array. + Cons: Requires modern JavaScript features (ECMAScript 2015+) and may not work in older browsers. **Library Used** There is no library used in this benchmark. The tests only use built-in JavaScript features. **Special JS Feature/ Syntax** There are no special JavaScript features or syntax used beyond what's required for the two options being compared. **Other Alternatives** If the spread operator were not available, an alternative could be using `Array.prototype.push()` to concatenate arrays: `var c = []; c.push.apply(c, a); c.push.apply(c, b);` Another option would be using `Array.prototype.reduce()` or `Array.prototype.forEach()` to achieve similar results. It's worth noting that in modern JavaScript, the spread operator is often preferred over concatenation because of its efficiency and simplicity. **Benchmark Preparation Code** The provided benchmark preparation code is empty. Typically, this code would include any necessary setup or initialization to prepare the test environment. **Latest Benchmark Result** The latest benchmark result shows the performance characteristics of both options for a specific browser (Firefox 83) on a desktop device running Ubuntu. The results indicate that spreading is generally faster than concatenation, which aligns with expectations based on the pros and cons discussed earlier.
Related benchmarks:
simple spread vs concat benchmark
Splice+Spread vs concat to concat arrays
unshift vs spread vs concat
spread vs concat for cloning array benchmark
spread vs concat for adding new value to array
Comments
Confirm delete:
Do you really want to delete benchmark?