The ECMA specification does not specify a bounding complexity, however, you can derive one from the specification's algorithms.

push is O(1), however, in practice it will encounter an O(N) copy costs at engine defined boundaries as the slot array needs to be reallocated. These boundaries are typically logarithmic.

pop is O(1) with a similar caveat to push but the O(N) copy is rarely encountered as it is often folded into garbage collection (e.g. a copying collector could only copy the used part of an array).

shift is at worst O(N) however it can, in specially cases, be implemented as O(1) at the cost of slowing down indexing so your mileage may vary.

slice is O(N) where N is end - start. Not a tremendous amount of optimization opportunity here without significantly slowing down writes to both arrays.

splice is, worst case, O(N). There are array storage techniques that divide N by a constant but they significantly slow down indexing. If an engine uses such techniques you might notice unusually slow operations as it switches between storage techniques triggered by access pattern changes.

One you didn't mention, is sort. It is, in the average case, O(N log N). However, depending on the algorithm chosen by the engine, you could get O(N^2) in some cases. For example, if the engine uses QuickSort (even with an late out to InsertionSort), it has well-known N^2 cases. This could be a source of DoS for your application. If this is a concern either limit the size of the arrays you sort (maybe merging the sub-arrays) or bail-out to HeapSort.

Answer from chuckj on Stack Overflow
Top answer
1 of 3
185

The ECMA specification does not specify a bounding complexity, however, you can derive one from the specification's algorithms.

push is O(1), however, in practice it will encounter an O(N) copy costs at engine defined boundaries as the slot array needs to be reallocated. These boundaries are typically logarithmic.

pop is O(1) with a similar caveat to push but the O(N) copy is rarely encountered as it is often folded into garbage collection (e.g. a copying collector could only copy the used part of an array).

shift is at worst O(N) however it can, in specially cases, be implemented as O(1) at the cost of slowing down indexing so your mileage may vary.

slice is O(N) where N is end - start. Not a tremendous amount of optimization opportunity here without significantly slowing down writes to both arrays.

splice is, worst case, O(N). There are array storage techniques that divide N by a constant but they significantly slow down indexing. If an engine uses such techniques you might notice unusually slow operations as it switches between storage techniques triggered by access pattern changes.

One you didn't mention, is sort. It is, in the average case, O(N log N). However, depending on the algorithm chosen by the engine, you could get O(N^2) in some cases. For example, if the engine uses QuickSort (even with an late out to InsertionSort), it has well-known N^2 cases. This could be a source of DoS for your application. If this is a concern either limit the size of the arrays you sort (maybe merging the sub-arrays) or bail-out to HeapSort.

2 of 3
17

in very simple words

push -> O(1)

pop -> O(1)

shift -> O(N)

slice -> O(N)

splice -> O(N)

Here is a complete explanation about time complexity of Arrays in JavaScript.

🌐
Medium
omken.medium.com › javascripts-push-pop-shift-and-unshift-array-methods-with-respect-to-big-o-notation-e129ac5464
JavaScript’s Push, Pop, Shift, and Unshift Array Methods With Big O Notation. | by Omkesh B. Kendre | Medium
November 28, 2022 - Time calculated unshift(): 0.5321000000238418 seconds. Time calculated pop(): 0 seconds. Time calculated shift(): 0.5927999999523162 seconds.
🌐
Reddit
reddit.com › r/learnjavascript › time complexity of shift/unshift
r/learnjavascript on Reddit: Time complexity of shift/unshift
March 28, 2018 -

I can't seem to find a solid answer on this subject. What are the time complexities of array.unshift and array.shift? Does JS use a linked list or queue/stack for arrays? I would think that the index of each element after a shift/unshift would have to be adjusted, making them linear methods, but I have also heard that because JS doesn't have c-style arrays, JS can simply shift the 'head' and 'tail' (and somehow change all of the references to each index) in constant time?

Can anyone clarify?

🌐
DEV Community
dev.to › liaowow › is-javascript-s-shift-method-a-performance-boost-5df5
JavaScript Shift: Is JavaScript's .shift() Method a Performance Boost? - DEV Community
June 13, 2020 - An engineer friend further explained that adding arrays together is an O(n + m) complexity: Arrays are fixed in size under the surface, so when you add them together, you are actually creating a new array big enough to hold them.
🌐
DEV Community
dev.to › tochi_ › javascript-array-methods-under-the-hood-unshift-and-shift-explained-3gd
JavaScript Array Methods Under the Hood: Unshift and Shift Explained - DEV Community
July 31, 2025 - So, it shifts every existing element one position to the right. Another way to do this is: const numArray = [2, 3, 4]; const jsUnshift = (arr, newItem) => { for (let i = arr.length; i > 0; i--) { arr[i] = arr[i - 1]; } arr[0] = newItem; return arr; }; console.log(jsUnshift(numArray, 1)); //result ==> [1,2,3,4] The time it takes to perform this operation grows with the size of the array. Hence, the time complexity is O(n).
🌐
Lannonbr
lannonbr.com › blog › 2020-01-27-shift-optimizations
Array.shift optimizations in Firefox's JS Engine
In 2017, though, some engineers ... of the array over by one. In terms of time complexity, this reduces a linear process to a constant process (O(n) to O(1))....
🌐
Mozilla Bugzilla
bugzilla.mozilla.org › show_bug.cgi
394448 - Array.shift() performance
Array.prototype.shift can probably be made somewhat faster, but to be completely honest, it's never going to be as efficient as you wish. The specification says that the function returns this[0] and shifts every element or hole down an index. The specification requires that [[Get]] and [[Put]] be called for each element in this, holes not included, so the time complexity of shifting elements off the front of the array to iterate over the array is quadratic.
🌐
Medium
medium.com › @ashfaqueahsan61 › time-complexities-of-common-array-operations-in-javascript-c11a6a65a168
Time Complexities Of Common Array Operations In JavaScript | by Ashfaque Ahsan | Medium
September 4, 2019 - Think of the indices as addresses to these elements in memory. Since we have the address of ‘C’ which is index 2, we can directly retrieve it without having to go through anything else. So that means accessing values of an array have a Constant Time Complexity which we can write as O(1).
🌐
CodingNomads
codingnomads.com › javascript-array-unshift-shift-pop-push
JavaScript Array Essentials: Using pop, push, shift, and unshift
On the other hand, .pop() and .push() have a time complexity of O(1), which means their execution time remains constant regardless of the array's size. Adding or removing an item from the end of the array doesn’t require repositioning the other elements, so these operations are generally much faster and do not scale with the size of the array. It's important that you, as a student, don't get hung up on efficiency. While it's true that .shift() and .unshift() are less efficient for large arrays, the difference is negligible for most practical applications.
Find elsewhere
🌐
Hacker News
news.ycombinator.com › item
Array.shift Optimizations in Firefox's JavaScript Engine (2020) | Hacker News
April 11, 2024 - Unfortunately, trying to predict O-notation bounds in JavaScript is a fool's errand. The spec usually (always?) doesn't mention expected bounds, so it can depend on the runtime and the heuristics the runtime uses to decide what underlying data structure to use.
🌐
Mblogs
mbloging.com › home › dsa › understanding time complexity of javascript array operations
Understanding Time Complexity of JavaScript Array Operations | Mbloging
April 15, 2025 - Inserting or removing elements at the beginning of an array using unshift or shift involves shifting all existing elements to accommodate the changes. Thus, these operations have a linear time complexity (O(n)), where n represents the array's length.
🌐
Frontend Masters
frontendmasters.com › courses › practical-algorithms › big-o-push-shift-unshift
Big O: Push, Shift, & Unshift - A Practical Guide to Algorithms with JavaScript | Frontend Masters
>> Bianca Gandolfo: What's the time complexity of push? We talked a little bit about that. Constant, mm-hm. >> Student1: [INAUDIBLE] it goes to the end. >> Bianca Gandolfo: Mm-hm, yep, we always just put it at the end, not a big deal. [00:00:22] Other languages, when you're actually doing memory allocation, this might not be a constant. But in JavaScript, we can reason that this is just a constant. We don't need to worry about initializing an array of a certain length and blah, blah, blah, and then keeping track and then expanding if it exceeds a certain length.
🌐
JavaScript in Plain English
javascript.plainenglish.io › under-the-hood-worst-case-complexities-workings-of-popular-js-array-methods-739d5fef314a
Worst Case Complexities & Workings of Popular JS Array Methods | JavaScript in Plain English
August 9, 2022 - This method is similar to C++’s memmove [5] which is used to shift data in an array. In the worst case, n-1 elements would be copied to a new position in the array. ... All modifications are in place and the original array is returned after being modified. The concat() method is used to merge two or more arrays which returns a shallow-copied new array.[7] ... As two arrays are merged this operation will take into account the time taken to merge the biggest list.
🌐
CodeKraft
abdulapopoola.com › 2019 › 09 › 09 › javascript-array-deep-dive-shift-and-unshift
Understanding JavaScript Array methods: Unshift and Shift – CodeKraft
October 9, 2021 - It is the complement of push which appends values to the end of the array. Unshift instead inserts them at the head of the array · Time Complexity: O(n) since every element needs to be moved.
Top answer
1 of 9
70

push() is faster.

js>function foo() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.unshift(1); return((new Date)-start)}
js>foo()
2190
js>function bar() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.push(1); return((new Date)-start)}
js>bar()
10

function foo() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.unshift(1); return((new Date)-start)}
console.log(foo())

function bar() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.push(1); return((new Date)-start)}
console.log(bar());


Update

The above does not take into consideration the order of the arrays. If you want to compare them properly, you must reverse the pushed array. However, push then reverse is still faster by ~10ms for me on chrome with this snippet:

var a=[]; 
var start = new Date; 
for (var i=0;i<100000;i++) {
  a.unshift(1);
}
var end = (new Date)-start;
console.log(`Unshift time: ${end}`);

var a=[];
var start = new Date;
for (var i=0;i<100000;i++) {
  a.push(1);
}

a.reverse();
var end = (new Date)-start;
console.log(`Push and reverse time: ${end}`);

2 of 9
29

The JavaScript language spec does not mandate the time complexity of these functions, as far as I know.

It is certainly possible to implement an array-like data structure (O(1) random access) with O(1) push and unshift operations. The C++ std::deque is an example. A Javascript implementation that used C++ deques to represent Javascript arrays internally would therefore have O(1) push and unshift operations.

But if you need to guarantee such time bounds, you will have to roll your own, like this:

http://code.stephenmorley.org/javascript/queues/

🌐
Morioh
morioh.com › p › 1fd55e73ab3d
Javascript: Arrays Under The Hood + Time Complexity
This article aims to show how JavaScript arrays are implemented under the hood and explore the time complexities of common Array methods (i.e pop, shift, unshift)
🌐
Medium
medium.com › @yaelfisher › learning-about-time-complexity-with-javascript-arrays-methods-b8c2a8ee2101
Learning about Time Complexity with Javascript Array Methods | by Yael Fisher | Medium
February 26, 2022 - The same goes to Array.shift(), which will remove an item straight from the first index of the array, and its time complexity is also O(n).
🌐
freeCodeCamp
freecodecamp.org › news › the-complexity-of-simple-algorithms-and-data-structures-in-javascript-11e25b29de1e
The complexity of simple algorithms and data structures in JS
March 18, 2019 - Whereas, linearly searching through an array via its index, as seen before, has a complexity of O(n). Also, because a shift or unshift of a value to or from the front of an array requires reindexing each element that follows it (i.e. removing ...
🌐
Hey
world.hey.com › mgmarlow › time-complexity-of-array-push-d950f9dc
Time complexity of Array.push
Do some light googling on Queues, ... Queue#dequeue is no surprise: Array.prototype.shift has a worst-case complexity of O(n), linear time....