Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unshift() vs push()
(version: 1)
Comparing performance of:
unshift vs push
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
unshift
let a = []; for(var i = 0; i < 100000; ++i) { a.unshift(i); }
push
let a = []; for(var i = 0; i < 100000; ++i) { a.push(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unshift
push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unshift
2.6 Ops/sec
push
5669.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON tests the performance of two JavaScript methods for manipulating arrays: `unshift()` and `push()`. Both methods are used to add elements to an array, but they do so in different ways and with varying performance implications. ### Description of Options Compared 1. **`Array.prototype.push()`**: - **Description**: This method adds one or more elements to the end of an array. - **Syntax**: `array.push(element1, element2, ..., elementN)` - **Test Case**: In the benchmark, the code snippet `for(var i = 0; i < 100000; ++i) { a.push(i); }` constructs an array by pushing integers 0 to 99,999 onto the end of the array. 2. **`Array.prototype.unshift()`**: - **Description**: This method adds one or more elements to the beginning of an array. - **Syntax**: `array.unshift(element1, element2, ..., elementN)` - **Test Case**: The benchmark for `unshift` is represented by `for(var i = 0; i < 100000; ++i) { a.unshift(i); }`, which results in adding integers 0 to 99,999 at the start of the array. ### Pros and Cons #### `push()` - **Pros**: - Generally more performant than `unshift()` for adding elements to an array, particularly in large arrays because it appends elements to the end without needing to reposition existing elements. - Is optimized in most JavaScript engines due to its common usage pattern. - **Cons**: - Limited to appending elements; cannot be used for inserting elements at the beginning of an array. #### `unshift()` - **Pros**: - Useful for scenarios where you need to insert elements at the start of the array. - Can be more intuitive for algorithms that require FIFO (First In, First Out) behavior, such as queues. - **Cons**: - Performance generally degrades as it involves shifting existing elements to make room for the new elements, which can be costly in terms of computational time especially as the size of the array increases. - May lead to higher time complexity (O(n)) when used on large arrays, making it less favorable for performance-critical applications. ### Other Considerations - **Execution Results**: In the benchmark results, `push()` had a significantly higher execution rate (approximately 908.75 executions per second) compared to `unshift()` (approximately 1.78 executions per second). This stark difference illustrates the performance disadvantage of `unshift()`. - **Use in Applications**: Choosing between `push()` and `unshift()` often depends on the specific use case or requirement of the application. If adding elements to the beginning of an array is necessary, `unshift()` remains relevant. ### Alternatives Some alternatives to consider for managing array operations include: - **Array concatenation** (`concat()`): For merging arrays but is less direct for inserting elements at specific points. - **Using linked lists**: A data structure that can efficiently add or remove items from both ends, especially suitable for applications needing frequent insertions/deletions. - **Using `Array.prototype.splice()`**: For inserting elements at any position but at the cost of performance, as it involves shifting multiple elements. - **Other data structures**: Depending on the requirements, using a deque (double-ended queue) or other optimized structures may provide better performance for certain scenarios. In summary, the benchmark compares `push()` and `unshift()`, showing a clear performance edge for `push()`, and emphasizes the importance of understanding the implications of array-manipulation methods in JavaScript.
Related benchmarks:
Spread vs Push
another push vs unshift
slice vs destruction with inserts
length vs push
Test Unshift vs push + reverse
unshift vs push
push vs length
unshift() vs push() vs fill()
unshift() vs push() vs allocate-fill()
Comments
Confirm delete:
Do you really want to delete benchmark?