Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push fork
(version: 1)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
Array.prototype.concat vs spread operator vs Push spread vs Push
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = Array(100); array.fill(1); var params = Array(100); params.fill(2);
Tests:
Array.prototype.concat
array = array.concat(params);
spread operator
array = [ ...array, ...params ]
Push spread
array.push(...params);
Push
for (let index = 0; index < params.length; ++index) array.push(params[index]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
Push spread
Push
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):
The provided JSON represents a benchmark test case for comparing the performance of three different approaches to concatenate arrays in JavaScript: `Array.prototype.concat()`, the spread operator (`...`), and the `push()` method with spreading. Let's break down each approach: 1. **Array.prototype.concat()**: This is the traditional way to concatenate two arrays in JavaScript. It creates a new array by iterating over both input arrays and appending elements from one array to another. 2. **Spread operator (`...`)**: Introduced in ES6, this syntax allows you to expand an iterable (such as an array) into individual elements, which can then be used within the context of another iterable. In this test case, it's used to create a new array by spreading one array and concatenating it with another. 3. **Push spread**: This approach uses the `push()` method in conjunction with the spread operator. Instead of directly pushing elements from an array, you spread an array into individual arguments for `push()`. The `push()` method then appends these elements to the end of the original array. Now, let's discuss the pros and cons of each approach: * **Array.prototype.concat()**: + Pros: Well-established and widely supported. + Cons: Creates a new array, which can lead to increased memory allocation and garbage collection overhead. + Considerations: This method is generally slow due to its iterative nature. However, modern browsers have optimized this method for performance. * **Spread operator (`...`)**: + Pros: Efficient, as it doesn't require iterating over the arrays; creates a new array with minimal memory allocation. + Cons: Less widely supported (IE11 and older), and might not be familiar to some developers. + Considerations: This method is generally faster than `concat()` due to its vectorized nature. However, modern browsers have optimized this method as well. * **Push spread**: + Pros: A balanced approach that combines the benefits of both `concat()` and spread operator; efficient and familiar syntax for developers. + Cons: Requires an extra step (spreading) before pushing elements; might be slightly slower than using the spread operator directly. When to use each approach: * Use `Array.prototype.concat()` when: + You need to concatenate arrays in older browsers or environments that don't support modern JavaScript features. + You're working with very large datasets, as this method is optimized for performance. * Use the **spread operator** when: + You want to create a new array efficiently and with minimal memory allocation. + You're targeting modern browsers (IE11 and newer). * Use **Push spread** when: + You need a balance between efficiency, readability, and compatibility. + You prefer a familiar syntax that's easy to understand. Other alternatives: * **Array.prototype.reduce()**: This method can be used to concatenate arrays in a more functional programming style. However, it's generally slower than the other approaches due to its recursive nature. * **Array.from() + spread operator**: This approach is similar to using the spread operator directly but uses `Array.from()` instead. It provides an alternative way to create an array with minimal memory allocation. The provided benchmark results show that the spread operator and Push spread are generally faster than Array.prototype.concat() in this specific test case, likely due to their vectorized nature and reduced overhead.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
spread operator vs push Brian
Array.prototype.concat vs spread operator (add)
Array concat vs spread operator vs push larger list
Array.prototype.concat vs spread operator (fix)
Comments
Confirm delete:
Do you really want to delete benchmark?