Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array#push vs Array#apply
(version: 0)
Comparing performance of: Array#push vs Array#apply
Comparing performance of:
Array#push vs Array#apply vs Array#push#apply
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array#push
let array1 = ['a', 'b', 'c']; let array2 = ['d', 'e', 'f']; array1.push(...array2);
Array#apply
let array1 = ['a', 'b', 'c']; let array2 = ['d', 'e', 'f']; Array.prototype.push.apply(array1, array2);
Array#push#apply
let array1 = ['a', 'b', 'c']; let array2 = ['d', 'e', 'f']; array1.push.apply(array1, array2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array#push
Array#apply
Array#push#apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array#push
62099228.0 Ops/sec
Array#apply
12740614.0 Ops/sec
Array#push#apply
59554916.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition** The benchmark is comparing the performance of two approaches to append elements to an array: `Array#push` and `Array#apply`. **Options Compared** 1. **Array#push**: This method appends one or more elements to the end of an array. 2. **Array#apply**: This method applies a specified function to all elements in an array, including the initial elements. **Pros and Cons** * **Array#push**: + Pros: Simple, efficient, and widely supported. + Cons: Can lead to shallow copying of large arrays, potentially causing performance issues. * **Array#apply**: + Pros: Can be more efficient for certain use cases (e.g., when dealing with large arrays). + Cons: Less intuitive and less widely supported compared to `Array#push`. **Library and Purpose** None of the test cases explicitly use a library. However, `Array.prototype` is used implicitly by defining methods on it (`array1.push` and `array1.push.apply`). **Special JS Feature or Syntax** None mentioned. Now, let's examine each individual test case: 1. **Array#push**: The benchmark definition shows an array being created with initial elements and then pushing additional elements onto it using the spread operator (`...`). This is a simple and efficient way to append elements. 2. **Array#apply**: This test case demonstrates applying the `push` method to another array as the first argument, followed by the original array as the second argument. This approach can be more efficient for certain use cases, but it's less intuitive than using `Array#push`. 3. **Array#push#apply**: In this test case, both `Array#push` and `Array#apply` are used together. The purpose of this variation is unclear, as it seems to combine the benefits of both approaches. **Other Alternatives** For appending elements to an array, other methods exist: * **concat()**: Concatenates one or more arrays. * **Spread operator (Rest parameter)**: Used with `Array#push`, `Array#concat`, or other array methods to append new elements. The choice of method depends on the specific use case and personal preference. In general, `Array#push` is a good starting point due to its simplicity and efficiency. In conclusion, this benchmark provides insight into the performance differences between using `Array#push` and `Array#apply`. While both methods have their advantages and disadvantages, `Array#push` remains a popular choice for appending elements to arrays.
Related benchmarks:
Array spread vs. push performance
Array .push() vs .unshift() vs spread
spread vs push - simple
Array .push() vs spread operator
Spread vs Push when adding into array
Comments
Confirm delete:
Do you really want to delete benchmark?