Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs Array.prototype.push vs spread operator
(version: 0)
Compare the new ES6 spread operator with the traditional concat() and push() method
Comparing performance of:
Array.prototype.concat vs spread operator vs Array.prototype.push
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params);
spread operator
var params = [ "hello", true, 7 ] var other = [ 1, 2, ...params ]
Array.prototype.push
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].push(...params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
Array.prototype.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):
**Overview** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The test compares the performance of three methods to concatenate arrays: `Array.prototype.concat`, `Array.prototype.push` with the spread operator (`...`), and the new ES6 spread operator. **Tested Options** 1. **`Array.prototype.concat"`**: This method takes two or more arrays as arguments and returns a new array containing all elements from each input array. 2. **`Array.prototype.push` with spread operator (`...`)**: This method takes one or more values to be added to the end of an array, followed by another value that is used for comparison purposes (not explicitly shown in this test case). The spread operator is used to pass multiple values to `push()`. 3. **New ES6 spread operator (`...`)**: This operator allows you to expand a source array into individual elements. **Pros and Cons of Each Approach** 1. **`Array.prototype.concat"`**: * Pros: Simple and efficient, can be used with any number of input arrays. * Cons: Creates a new array object, which can lead to memory allocation overhead. 2. **`Array.prototype.push` with spread operator (`...`)**: * Pros: More flexible than `concat()`, as it allows for dynamic addition of elements. * Cons: Can be slower due to the overhead of comparing values using `push()` and spread operator. 3. **New ES6 spread operator (`...`)**: * Pros: More concise and expressive, can lead to better code readability. * Cons: Only works with arrays as input, may not be suitable for all use cases. **Library** None of the test cases explicitly uses a library. However, it's worth noting that `Array.prototype.concat()` is implemented in the JavaScript standard library, while `push()` and the spread operator are also part of the standard library. **Special JS Feature or Syntax** The test case does not explicitly use any special JavaScript features or syntax beyond what is commonly used in JavaScript programming. However, it's worth noting that the spread operator was introduced in ECMAScript 2015 (ES6) and has become a widely adopted feature in modern JavaScript development. **Other Alternatives** If you're interested in exploring alternative methods for array concatenation, some other options include: 1. **`Array.prototype.reduce()`**: This method can be used to concatenate arrays by using the `reduce()` function with an initial value of an empty array. 2. **`Array.prototype.concat()` with a loop**: Instead of passing multiple arrays to `concat()`, you can use a loop to iterate over each array and concatenate its elements. Keep in mind that these alternatives may have different performance characteristics or trade-offs compared to the methods tested in this benchmark.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (add)
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator on large array
Array.prototype.concat vs spread operator on small array
Comments
Confirm delete:
Do you really want to delete benchmark?