Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs push spread vs push apply
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
concat vs push spread vs push apply
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var params = []; for(let i=0; i<1000; ++i) { params.push(i); }
Tests:
concat
var other = params.slice(); other.concat(params);
push spread
var other = params.slice(); other.push(...params);
push apply
var other = params.slice(); other.push.apply(other, params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
concat
push spread
push apply
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 provided JSON and explain what is being tested. **Benchmark Definition** The benchmark compares three approaches to concatenate an array in JavaScript: 1. `push` with spread operator (`...`) 2. `push.apply()` 3. `concat()` method These methods are compared because they have different performance characteristics, and it's interesting to see which one comes out on top. **Options Compared** The options being compared are: * `push` with spread operator (`...`): This is a modern JavaScript feature that allows you to pass multiple arguments to the `push()` method using the spread operator. It's a concise way to add elements to an array. * `push.apply()`: This method calls `push()` on the target array, passing each argument individually. It's a more traditional approach. * `concat()`: This is another built-in JavaScript method that concatenates two arrays and returns a new array. **Pros and Cons** Here are some pros and cons of each approach: * **`push` with spread operator (`...`)**: + Pros: concise, readable, and easy to use. + Cons: might be slower due to the overhead of evaluating the spread operator. * **`push.apply()`**: + Pros: can be faster because it avoids the overhead of the spread operator evaluation. + Cons: more verbose and less readable than `push` with spread operator. * **`concat()`**: + Pros: fast and efficient, but might require more memory allocation for the intermediate array. + Cons: less concise and readable than `push` with spread operator. **Libraries and Special JS Features** There are no libraries mentioned in the provided JSON. However, it's worth noting that some browsers might use their own implementations of JavaScript features, such as `let` and `const`, which were introduced in ECMAScript 2015 (ES6). **Benchmark Preparation Code** The script preparation code creates an array `params` with 1000 elements using a simple loop. **Individual Test Cases** Each test case is defined by the following lines: * `"var other = params.slice();\nother.concat(params);"`: This line concatenates the `params` array to the `other` array. * `"var other = params.slice();\nother.push(...params);"`: This line adds all elements of `params` to `other` using the spread operator. * `"var other = params.slice();\nother.push.apply(other, params);"`: This line calls `push()` on `other`, passing each element of `params` individually. **Benchmark Result** The latest benchmark result shows that: * `push apply` is faster than `concat()`. * `push spread` is the fastest among the three options. Overall, the test suggests that `push apply` might be a better option for concatenating arrays when performance is critical. However, it's essential to keep in mind that these results might depend on specific use cases and browser implementations.
Related benchmarks:
Array concat vs spread operator vs push reassign v2
spread operator vs push Brian
Array concat vs spread operator vs push (Super Big Array)
Array concat vs spread operator vs push fork
Array concat vs spread operator vs push larger list
Comments
Confirm delete:
Do you really want to delete benchmark?