Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs spread vs concat
(version: 0)
Comparing performance of:
spread vs push vs concat
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
spread
let foo = []; let bar = [1,2,3,4,5,6,7,8,9,10]; for(let i = 0; i < 10; i++) foo = [...foo, ...bar];
push
let foo = []; let bar = [1,2,3,4,5,6,7,8,9,10]; for(let i = 0; i < 10; i++) foo.push(...bar);
concat
let foo = []; let bar = [1,2,3,4,5,6,7,8,9,10]; for(let i = 0; i < 10; i++) foo = foo.concat(bar);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread
push
concat
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 Overview** The benchmark is comparing three ways to append elements to an array in JavaScript: `push`, `spread` (using the spread operator), and `concat`. The test creates an initial empty array, populates it with a fixed set of values, and then uses a loop to append these values to the end of the array using each of the three methods. **Test Options Compared** 1. **`push()`**: This method appends one or more elements to the end of an array. 2. **`spread()` (using the spread operator `...`)**: This method creates a new array by spreading the elements of another array. 3. **`concat()`**: This method returns a new array containing the elements of the original array and then the elements of any additional arrays. **Pros and Cons of Each Approach** 1. **`push()`**: * Pros: Fast, efficient, and widely supported. * Cons: Can be slower than `spread()` for large arrays because it involves an array mutation operation. 2. **`spread()` (using the spread operator `...`)**: * Pros: Modern, efficient, and easy to read. * Cons: Requires a modern JavaScript engine that supports the spread operator. 3. **`concat()`**: * Pros: Wide support across older browsers and engines. * Cons: Can be slower than `push()` because it involves an array mutation operation and creates a new array. **Library Usage** None of the test cases use any external libraries or dependencies, other than the built-in JavaScript language features. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax that require advanced knowledge of JavaScript. The spread operator (`...`) is used to create a new array by spreading elements from another array, which is a relatively modern feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** If you were to write this benchmark yourself, you could also consider using other methods for appending elements to an array, such as: * Using `Array.prototype.push.apply()` or `Array.prototype.unshift()` * Creating a new array with the `Array.from()` method and then using `push()` or `concat()` * Using a library like Lodash's `_.union()` method However, these alternatives are less common and may not be as widely supported across different browsers and engines.
Related benchmarks:
Array concat vs spread operator vs push #3
spread operator vs push Brian
spread operator vs push Brian2
Array concat vs spread operator vs push larger list
zk test spread vs push
Comments
Confirm delete:
Do you really want to delete benchmark?