Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push123123
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push
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 = [ "hello", true, 7 ]; var d = [1, 2] var other = d.concat(params);
spread operator
var params = [ "hello", true, 7 ] var d = [1, 2] var other = [ ...d, ...params ]
Push
var params = [ "hello", true, 7 ]; var d = [1, 2] var other = d.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
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 dive into the details of this JavaScript microbenchmark. **Overview** The benchmark is comparing three ways to concatenate arrays in JavaScript: 1. `Array.prototype.concat()` 2. The new ES6 spread operator (`...`) 3. The `push()` method with the rest parameter syntax (`...`) **Benchmark Definition** The provided JSON defines a single benchmark with three individual test cases. Each test case represents a specific implementation of array concatenation. **Test Cases** 1. **Array.prototype.concat()** This test case uses the traditional `concat()` method to concatenate two arrays, `[1, 2]` and `params`, which contains three elements: `"hello"`, `true`, and `7`. ```javascript var params = [ "hello", true, 7 ]; var d = [1, 2]; var other = d.concat(params); ``` **Pros and Cons** * Pros: + Widely supported by most browsers and JavaScript engines. + Easy to understand and use for developers familiar with the method. * Cons: + Creates a new array object, which can be memory-intensive for large arrays. + May not be as efficient as other methods in modern JavaScript engines. 2. **Spread Operator (`...`)** This test case uses the spread operator to concatenate two arrays: `[1, 2]` and `params`. The syntax is: ```javascript var params = [ "hello", true, 7 ]; var d = [1, 2]; var other = [ ...d, ...params ]; ``` **Pros and Cons** * Pros: + Creates a new array object with minimal overhead. + More concise and expressive than traditional `concat()` method. * Cons: + May not be supported by older browsers or JavaScript engines (though this benchmark's results suggest it's well-supported). + Can be less intuitive for developers unfamiliar with the syntax. 3. **Push (`...`)** This test case uses the `push()` method with the rest parameter syntax to concatenate two arrays: `[1, 2]` and `params`. The syntax is: ```javascript var params = [ "hello", true, 7 ]; var d = [1, 2]; var other = d.push(...params); ``` **Pros and Cons** * Pros: + Creates a new array object with minimal overhead. + More concise than traditional `concat()` method. * Cons: + May not be supported by older browsers or JavaScript engines (though this benchmark's results suggest it's well-supported). + Can be less intuitive for developers unfamiliar with the syntax. **Library and Special JS Features** None of these test cases rely on external libraries. However, they do utilize a special JS feature: rest parameter syntax (`...`). This was introduced in ECMAScript 2015 (ES6) and allows functions to accept variable numbers of arguments. **Benchmark Results** The provided benchmark results show the number of executions per second for each test case: * **Array.prototype.concat()**: 6697644.5 * **Spread Operator (`...`)**: 21206516.0 * **Push (`...`)**: 23251886.0 These results suggest that the push method is currently the fastest, followed closely by the spread operator. However, it's essential to note that benchmarking results can vary depending on specific use cases and JavaScript engines. **Other Alternatives** There are other ways to concatenate arrays in JavaScript, such as using `Array.prototype.reduce()` or `Array.prototype.flatMap()`. However, these methods may not be as widely supported or efficient as the three test cases mentioned above.
Related benchmarks:
Array.prototype.concat vs spread operator
Array.prototype.concat vs Spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
ES6 Array concat vs spread operator
Array.prototype.concat vs spread operator real
Comments
Confirm delete:
Do you really want to delete benchmark?