Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs .concat()
(version: 0)
Comparing performance of:
Spread vs .concat()
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params);
Tests:
Spread
const params = [ "hello", true, 7 ] const other = [ 1, 2, ...params ]
.concat()
const params = [ "hello", true, 7 ]; const other = [ 1, 2 ].concat(params);
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
56884820.0 Ops/sec
.concat()
16287786.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark defines two test cases: "Spread" and ".concat()". It provides scripts for preparing data and HTML code for rendering the results. **Script Preparation Code** For both tests, the script preparation code is identical: ```javascript var params = [ "hello", true, 7 ]; ``` This code creates an array `params` with three elements: a string `"hello"`, a boolean `true`, and a number `7`. **HTML Preparation Code** There is no HTML preparation code for this benchmark. **Individual Test Cases** The benchmark defines two test cases: 1. **Spread**: The benchmark definition is: ```javascript const params = [ "hello", true, 7 ]; const other = [ 1, 2, ...params ]; ``` This test case uses the spread operator (`...`) to create a new array `other` by copying all elements from `params`. 2. **.concat()**: The benchmark definition is: ```javascript const params = [ "hello", true, 7 ]; const other = [ 1, 2 ].concat(params); ``` This test case uses the `.concat()` method to concatenate two arrays: `[1, 2]` and `params`. **Pros and Cons of Different Approaches** Here's a brief analysis of both approaches: * **Spread**: Using the spread operator has several advantages: + It creates a shallow copy of the original array. + It's faster than `.concat()` because it avoids creating an intermediate array. + It's more memory-efficient because it doesn't require allocating additional memory for an intermediate array. However, it may not work as expected with non-array objects or arrays with functions as elements. * **.concat()**: Using `.concat()` has some advantages: + It works with any type of iterable (not just arrays). + It's often more readable and familiar to developers who are used to using `+` for concatenation. However, it creates an intermediate array, which can lead to higher memory usage and slower performance compared to the spread operator. **Other Considerations** In this benchmark, the difference between the two approaches is likely due to the way JavaScript optimizes array iteration. The spread operator may be faster because it avoids creating an intermediate array, while `.concat()` may create an intermediate array that gets copied during iteration. **Library and Special JS Features** Neither of these tests uses any external libraries or special JavaScript features. They are basic examples of using arrays in JavaScript. **Alternatives** If you want to optimize your code for performance, you can explore other approaches: * Use `Array.prototype spread` (ES6) instead of the spread operator (`...`) for a more efficient and concise solution. * Use `concat()` with an intermediate array if you need to support older browsers or environments that don't have ES6 features. Keep in mind that benchmarking results can vary depending on the specific JavaScript engine, platform, and other factors. It's essential to run multiple benchmarks and consider multiple factors when optimizing your code for performance.
Related benchmarks:
spread vs concat xxx2
Array.prototype.concat vs spread operator new 2020
spread operator vs concat
concat vs spread with js array testing 1.2.3.
Array.prototype.concat vs spread operator 99
Comments
Confirm delete:
Do you really want to delete benchmark?