Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push (for single item)
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push for single item
Comparing performance of:
Array.prototype.concat vs spread operator vs Push
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ 1, 2 ]; var other = params.concat([3]);
spread operator
var params = [ 1, 2 ] var other = [ ...params, 3 ]
Push
var params = [ 1, 2 ]; var other = params.push(3);
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
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):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark compares the performance of three approaches to concatenate an array with a single item: 1. `concat()` method (traditional approach) 2. Spread operator (`...`) 3. `push()` method **Options Compared** * `concat()`: This is the traditional way to concatenate arrays in JavaScript. It creates a new array and copies elements from the original array into it. * `spread operator` (`...`): Introduced in ES6, this operator allows you to create a new array by spreading an existing array or values. * `push()`: This method adds one or more elements to the end of an array. **Pros and Cons** 1. **concat()**: * Pros: Easy to understand, widely supported, works well with large arrays. * Cons: Creates a new array, can be slower for small arrays, uses more memory. 2. **spread operator` (`...`): * Pros: Efficient, creates a new array but reuses existing one's elements, easy to read and write. * Cons: Requires ES6 support, might cause confusion among developers without experience with it. 3. **push()**: * Pros: Fast, efficient, uses less memory than `concat()`. * Cons: Can be slower for very small arrays (single element), has limited use cases. **Library Used** None mentioned in the benchmark definition. However, some browsers may include additional libraries or utilities that might affect performance. **Special JS Features/Syntax** The spread operator (`...`) is a new JavaScript feature introduced in ES6. It allows you to create a new array by spreading an existing array or values. **Other Alternatives** 1. `Array.prototype.slice()`: This method creates a shallow copy of an array, which can be used for concatenation. 2. `Array.prototype.join()`**: This method concatenates all elements of an array into a single string. 3. Other methods like `Array.prototype.reduce()` or custom functions might also be used for array concatenation. Keep in mind that this benchmark is focused on the specific use case of concatenating an array with a single item, so other alternatives may not be as relevant or efficient in this context.
Related benchmarks:
Array.prototype.concat vs spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
ES6 Array concat vs spread operator
Array concat vs spread operator vs push #3
Array.prototype.concat vs spread operator 12
Comments
Confirm delete:
Do you really want to delete benchmark?