Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs push.apply vs const push spread vs let push spread vs reassign spread
(version: 0)
Comparing performance of:
push vs push.apply vs const push spread vs reassign spread vs let push spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
push
let a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; values.forEach(value => { a.push(value); }); } console.log(a);
push.apply
let a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; Array.prototype.push.apply(a, values); } console.log(a);
const push spread
const a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; a.push(...values); } console.log(a);
reassign spread
let a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; a = [...a, ...values]; } console.log(a);
let push spread
let a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; a.push(...values); } console.log(a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
push
push.apply
const push spread
reassign spread
let push spread
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 what's being tested in the provided benchmark. **Benchmark Overview** The benchmark compares the performance of four different approaches to push elements onto an array: 1. `let a = []` and then using `push()` 2. `const a = []` and then using `push()` (with spread syntax) 3. `let a = []` and then using `push(...values)` 4. Reassigning the `a` variable to a new array with spread syntax (`a = [...newArray, ...values]`) **Options Compared** The benchmark is comparing the performance of these four approaches: * **`let a = []` + `push()`**: Using a mutable variable `a` and pushing elements onto it using the `push()` method. * **`const a = []` + push()`**: Using an immutable variable `a` and pushing elements onto it using the `push()` method. Note that this approach is less efficient because `a` cannot be reassigned. * **`let a = []` + push(...)`**: Using a mutable variable `a` and pushing elements onto it using the spread syntax (`...values`). This approach is similar to the first one but uses spread syntax instead of `push()`. * **Reassigning spread** (`a = [...newArray, ...values]`): Reassigning the `a` variable to a new array with spread syntax. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`let a = []` + `push()`**: Pros: Simple and easy to understand. Cons: Less efficient due to repeated reassignments. 2. **`const a = []` + push()`**: Pros: Immutable, which can be beneficial for performance and memory safety. Cons: Less efficient due to constant reassignments. 3. **`let a = []` + push(...)`**: Pros: Similar to the first approach but with spread syntax, which can be more concise. Cons: Still less efficient than reassigning. 4. **Reassigning spread** (`a = [...newArray, ...values]`): Pros: Most efficient due to minimizing the number of assignments and using a single operation to create the new array. Cons: Requires creating a new array on each iteration. **Library and Special JS Features** The benchmark uses the `push()` method, which is a part of the Array prototype in JavaScript. The spread syntax (`...`) is also used, but it's not specific to any library or feature. **Other Alternatives** If you want to test other approaches, here are some alternatives: * Using `unshift()` instead of `push()` * Using `splice()` instead of `push()` * Using `concat()` with spread syntax (`[...array].concat(values)`) * Using a different data structure, such as a linked list or a tree However, the benchmark is already testing some common and efficient approaches, so it's likely that these alternatives wouldn't show significant differences in performance.
Related benchmarks:
Array .push() vs .unshift() vs spread
spread operator vs push Brian
spread operator vs push Brian2
spread vs push - simple
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?