Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Spread vs concat to let var
(version: 0)
Benchmark.js
Comparing performance of:
concat vs spread
Created:
2 years ago
by:
Guest
Go to the latest result
Tests:
concat
let p = [1,2,3,4]; p = p.concat([4,5,6,7])
spread
let p = [1,2,3,4]; p = [...p, ...[4,5,6,7]];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
concat
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
spread
33.3M/s
concat
11.2M/s
View exact numbers
Test name
Executions per second
✓
spread
33,256,466 Ops/sec
concat
11,182,829 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark is testing two different approaches to create an array by concatenating two arrays: using the `concat()` method and using the spread operator (`...`). **Test Cases** There are only two test cases: 1. **"concat"`**: This test case uses the `concat()` method to concatenate two arrays. ```javascript let p = [1, 2, 3, 4]; p = p.concat([4, 5, 6, 7]); ``` 2. **"spread"`**: This test case uses the spread operator (`...`) to create a new array by spreading two existing arrays together. ```javascript let p = [1, 2, 3, 4]; p = [...p, ...[4, 5, 6, 7]]; ``` **Comparison** The benchmark is comparing the performance of these two approaches. The `concat()` method creates a new array by copying elements from one array to another, while the spread operator creates a new array by taking elements from existing arrays and concatenating them. **Pros and Cons** * **`concat()` method**: + Pros: widely supported, easy to use. + Cons: creates a new array, can be slower than using the spread operator. * **Spread operator (`...`)**: + Pros: more concise, can be faster than `concat()`. + Cons: not supported in older browsers or environments. **Library and Special JS Features** Neither of these approaches uses any libraries. However, it's worth noting that some browsers (like Edge) use a special JavaScript feature called "rest parameter" (`...`) which allows for spreading arrays into an array initializer. This is used in the `spread` test case. **Other Considerations** * **Array creation**: Both approaches create a new array, but the spread operator can be more efficient because it only creates a reference to the existing arrays, whereas `concat()` creates a copy of the elements. * **Browser support**: The spread operator is not supported in older browsers like Internet Explorer or Safari versions prior to 10.1. **Alternatives** If you want to create an array by concatenating two arrays, you could also use other methods, such as: * Using `Array.prototype.push()` and assigning the result to a variable. ```javascript let p = [1, 2, 3, 4]; p.push(...[4, 5, 6, 7]); ``` * Using `Array.prototype.concat()` with an array initializer. ```javascript let p = [1, 2, 3, 4].concat([4, 5, 6, 7]); ``` However, the spread operator (`...`) is generally considered the most concise and efficient way to create a new array from existing arrays.
Comments
Confirm delete:
Do you really want to delete benchmark?