Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Push vs Unshift
(version: 1)
Comparing performance of:
.push() vs .unshift()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = []
Tests:
.push()
arr.push({name: "lorem1", label: "lorem1"}, {name: "lorem2", label: "lorem2"}, {name: "lorem3", label: "lorem3"})
.unshift()
arr.unshift({name: "lorem1", label: "lorem1"}, {name: "lorem2", label: "lorem2"}, {name: "lorem3", label: "lorem3"})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.push()
.unshift()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.push()
5232815.0 Ops/sec
.unshift()
4856.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares two JavaScript array methods: `.push()` and `.unshift()`. Here's a breakdown of what each method does, their performance implications, and considerations surrounding their use: ### Methods Being Tested 1. **.push()** - **Functionality**: This method adds one or more elements to the end of an array. - **Use Case**: Suitable for scenarios where you need to append items to a collection. 2. **.unshift()** - **Functionality**: This method adds one or more elements to the beginning of an array. - **Use Case**: Used when you need to insert items at the start of the array. ### Performance Results From the latest benchmark results: - The `.push()` method demonstrated a significantly higher execution rate at approximately **1,892,182 executions per second**. - The `.unshift()` method, on the other hand, showed only about **8.83 executions per second**. ### Pros and Cons - **.push()** - **Pros**: - High performance, making it suitable for frequent operations. - Simple to use for adding elements sequentially. - **Cons**: - Only adds elements to the end of the array, which may not be suitable for all use cases (e.g., if you want a FIFO data structure). - **.unshift()** - **Pros**: - Allows inserting elements at the start of the array, which can be useful for certain data structures like stacks or queues. - **Cons**: - Low performance compared to `.push()`, particularly as the array grows larger. It often requires shifting all existing elements to accommodate the new ones, which can be computationally expensive. ### Other Considerations - **Memory Management**: Both `.push()` and `.unshift()` manipulate the array in place; however, due to the way arrays are structured in JavaScript, using `.unshift()` can lead to more memory overhead, particularly with larger arrays. - **Alternatives**: - Instead of using arrays, other data structures such as linked lists may offer different performance characteristics when it comes to adding items at specific points. However, linked lists are not natively available in JavaScript and would require implementing or using a library. - If you frequently need to add items at both ends of a collection, consider using `Deque` (double-ended queue) implementations, which may provide better performance characteristics in such scenarios. ### Conclusion This benchmark effectively highlights the performance differences between two fundamental operations on JavaScript arrays. Generally, developers should prefer `.push()` for appending elements, especially in performance-sensitive applications. The choice between these methods depends on the specific requirements of the algorithm and data structure used in the project.
Related benchmarks:
Array .push() vs .unshift()
Array .push() vs .unshift()
push vs unshift
push vs unshift
Array .push() vs .unshift() 2
12dsds
Array .push() vs .unshift() |
arr unshift vs push
spread vs push 001
Comments
Confirm delete:
Do you really want to delete benchmark?