Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.push.apply(arr, params) vs arr.push(...params)
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.push.apply(arr, params)
var arr = [ 1, 2, "world" ]; var params = [ "hello", true, 7 ]; Array.prototype.push.apply(arr, params);
arr.push(...params)
var arr = [ 1, 2, "world" ]; var params = [ "hello", true, 7 ]; params.push(...arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.push.apply(arr, params)
arr.push(...params)
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 is being tested. **Benchmark Definition** The benchmark compares two approaches for concatenating arrays in JavaScript: 1. `Array.prototype.concat()` 2. The new ES6 spread operator (`...`) **Options Compared** The benchmark tests the execution time of two different methods to concatenate an array `arr` with another array `params`. The options are: a. Using `Array.prototype.push.apply(arr, params)`: This method uses the `apply()` method to apply the `push()` method to `arr`, passing `params` as the argument. b. Using the spread operator (`...`) with `arr.push(...params)`: This method uses the spread operator to create a new array by spreading `params` and then pushes the resulting array into `arr`. **Pros and Cons** 1. **`Array.prototype.concat()`**: * Pros: Well-supported, widely implemented. * Cons: Creates a new array, which can be inefficient for large arrays. 2. **The ES6 spread operator (`...`) with `arr.push(...params)`**: * Pros: Efficient, creates an in-place modification, and is widely supported. * Cons: Requires JavaScript 5+ (ES6) support. **Other Considerations** * The benchmark does not account for array size variations. If the array sizes are different between the two methods, this could affect performance. * The benchmark assumes that `params` is a large array. In practice, if `params` is small or empty, one of the methods might be faster. **Library Usage** None of the provided benchmark test cases use any external libraries beyond the built-in JavaScript functions. **Special JS Feature/Syntax** The ES6 spread operator (`...`) is used in the second test case. This feature was introduced in ECMAScript 2015 (ES6) and allows for more concise array creation and modification. **Other Alternatives** If you want to compare other methods for array concatenation, consider using: * `Array.prototype.concat()` * `Array.prototype.push()`, multiple times * `Array.prototype.unshift()` * A library function like Lodash's `concat()` or a custom implementation
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator real
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?