Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
In place array concatenation benchmark.
(version: 2)
Test performance between spread operator and push.apply
Comparing performance of:
Spread Operator vs Push Apply
Created:
8 years ago
by:
Registered User
Jump to the latest result
Tests:
Spread Operator
var arr1 = [1,2,3]; var arr2 = [4,5,6]; arr1.push(...arr2);
Push Apply
var arr1 = [1,2,3]; var arr2 = [4,5,6]; arr1.push.apply(arr1, arr2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread Operator
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches for concatenating arrays in JavaScript: the spread operator (`...`) and `push.apply()`. **Options Compared** Two options are compared: 1. **Spread Operator**: This approach uses the `...` operator to expand the array `arr2` into separate arguments, which are then pushed onto `arr1`. 2. **Push Apply**: This approach uses the `apply()` method of the `push()` function to pass `arr2` as an array of arguments to be executed by `push()`. **Pros and Cons** * **Spread Operator** + Pros: More concise, easier to read, and often faster due to optimized JavaScript engine implementations. + Cons: May not work in older browsers or environments that don't support the spread operator (e.g., Internet Explorer 11). * **Push Apply** + Pros: Works in all modern browsers and environments, including older ones. + Cons: Less concise, harder to read, and may be slower due to the overhead of `apply()`. **Library Used** None. This benchmark is a simple JavaScript microbenchmark that uses built-in JavaScript features. **Special JS Feature or Syntax** The spread operator (`...`) was introduced in ECMAScript 2015 (ES6) and has since become a standard feature in modern browsers. It's used to expand arrays into separate arguments, allowing for more concise and readable code. **Other Alternatives** If you need to concatenate arrays in JavaScript before a certain version of ES6, you might consider using the following alternatives: * `concat()`: This method concatenates two or more arrays and returns a new array. It's less efficient than the spread operator but works in older browsers. * Array.prototype.push.call(): This approach uses the `call()` method to pass an array of arguments to `push()`, similar to `apply()`. However, it's less concise and harder to read. In summary, if you need to concatenate arrays in JavaScript, the spread operator is often a good choice due to its conciseness and potential performance benefits. If you need to support older browsers or environments, you may need to use an alternative approach like `concat()` or `push.call()`.
Related benchmarks:
Array.push vs Spread operator
Push vs LHS spread
JS array spread operator vs push
Array .push() vs spread operator
Array concat vs spread operator vs push + spread 2023-08-21
Comments
Confirm delete:
Do you really want to delete benchmark?