Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs apply.push vs spread
(version: 0)
Comparing performance of:
push vs push.apply vs push spread vs reassign 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); }); }
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); }
push spread
let a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; a.push(...values); }
reassign spread
let a = []; for (let i = 0; i < 1000; i += 1) { const values = [i, 10000 + i, 100000 + i]; a = [...a, ...values]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
push
push.apply
push spread
reassign 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):
I'll explain the benchmark in detail. **Overview** The provided JSON represents a JavaScript microbenchmark test case named "push vs apply.push vs spread". The test compares the performance of three different approaches to push elements onto an array: 1. Using the `push` method directly on the array. 2. Using the `apply` method with `Array.prototype.push`. 3. Using the spread operator (`...`) to create a new array and then reassigning it to the original array. **Options Compared** The three options being compared are: 1. **Direct push**: Using the `push` method directly on the array, e.g., `a.push(value);`. 2. **Apply with push**: Using `Array.prototype.push.apply(a, values)`, where `values` is an array of elements to be pushed onto `a`. 3. **Spread operator and reassign**: Using the spread operator (`...`) to create a new array and then reassigning it to the original array, e.g., `a = [...a, ...values];`. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Direct push**: * Pros: Simple and intuitive, no additional overhead. * Cons: May lead to slower performance due to repeated function calls and array resizing. 2. **Apply with push**: * Pros: More efficient than direct push since it uses a native method that is optimized for performance. * Cons: Requires passing the array as an argument, which can be less convenient in some cases. 3. **Spread operator and reassign**: * Pros: Creates a new array without modifying the original one, which can improve performance in certain scenarios. * Cons: Requires additional memory allocation and copying of elements. **Library and Special JS Features** None of the test cases use any external libraries or special JavaScript features (e.g., ES6 classes, async/await). **Benchmark Results** The latest benchmark results show that the "push spread" approach outperforms the other two options, followed closely by "apply with push". The results suggest that using the spread operator and reassigning can be an efficient way to add elements to an array without modifying its original state. **Other Alternatives** Some alternative approaches to pushing elements onto an array include: 1. Using `unshift` instead of `push`. 2. Using a loop with indexing (e.g., `a[i++] = value;`). 3. Using `concat` to concatenate arrays (although this is generally less efficient than the spread operator). However, these alternatives are not part of the provided benchmark test case, and their performance characteristics may vary depending on specific use cases and JavaScript engines.
Related benchmarks:
Array .push() vs .unshift() vs spread
Pushing items via Array.push vs. Spread Operator
spread vs push - simple
JS array spread operator vs push
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?