Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs concat vs unshift (fair) to join arrays
(version: 2)
Comparing the performance of spread, vs concat vs unshift to join two arrays
Comparing performance of:
spread vs concat vs unshift (forEach) vs unshift (spread) vs push (forEach) vs push (spread)
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = [1,2,3]; var b = [4,5,6];
Tests:
spread
b=[...a,...b];
concat
b=a.concat(b);
unshift (forEach)
var c = []; a.forEach(el => c.unshift(el)) b.forEach(el => c.unshift(el))
unshift (spread)
var c = []; c.unshift(...a); c.unshift(...b);
push (forEach)
var c = []; a.forEach(el => c.push(el)) b.forEach(el => c.push(el))
push (spread)
var c = []; c.push(...a); c.push(...b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
spread
concat
unshift (forEach)
unshift (spread)
push (forEach)
push (spread)
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):
Measuring the performance of different array concatenation methods is crucial in JavaScript development, as it can impact the overall performance and scalability of applications. The provided benchmark compares four methods to join two arrays: 1. **Spread Method (b = [...a, ...b])**: This method uses the spread operator (`...`) to create a new array by spreading the elements of `a` and `b`. The pros of this method include its simplicity and readability. However, it may incur a higher overhead due to the creation of intermediate arrays. 2. **Concat Method (b = a.concat(b))**: This method uses the `concat()` method to concatenate two arrays. The `concat()` method creates a new array by copying elements from one or more source arrays. The pros of this method include its efficiency in joining two arrays. However, it may have higher overhead due to the creation of intermediate arrays. 3. **Unshift Method (a.forEach(el => c.unshift(el)), b.forEach(el => c.unshift(el)))**: This method uses a loop with `forEach()` and `unshift()` methods to concatenate two arrays. The pros of this method include its flexibility in handling multiple array concatenations. However, it may have higher overhead due to the creation of intermediate arrays and loop iterations. 4. **Unshift Spread Method (c = [...a], c.unshift(...b))**: This method uses a combination of spread operator (`...`) and `unshift()` method to concatenate two arrays. The pros of this method include its simplicity and efficiency in joining two arrays. However, it may incur higher overhead due to the creation of intermediate arrays. 5. **Push Method (forEach (el => c.push(el)), push(...a), push(...b))**: This method uses a loop with `forEach()` and `push()` methods to concatenate two arrays. The pros of this method include its flexibility in handling multiple array concatenations. However, it may have higher overhead due to the creation of intermediate arrays and loop iterations. In terms of performance, the benchmark results show that: * **Push Spread Method** (last row) is the fastest at 141 executions per second. * **Unshift Spread Method** (third row) is relatively fast at 1332 executions per second but with a lower number of repetitions for each execution. * The order of methods may vary based on specific implementation, JavaScript engine and hardware specifics. The pros and cons of these different approaches are: * **Efficiency**: Methods that avoid intermediate array creation or minimize loop iterations tend to be more efficient. * **Readability**: Simple and concise methods with fewer lines of code tend to be easier to read and maintain. * **Flexibility**: Methods that can handle multiple array concatenations or edge cases may be preferred in certain situations. Overall, the choice of method depends on the specific requirements of the application, including performance, readability, and maintainability considerations.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
unshift vs spread vs concat
.concat vs. spread
spread vs concat vs unshift to join arrays
Spread vs concat vs unshift (fair) to join arrays test
Comments
Confirm delete:
Do you really want to delete benchmark?