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
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 - The Array.push() has a Constant Time Complexity and so is O(1). All it does is add an element and give it an index thatโ€™s 1 greater than the index of the last element in the array.
Discussions

arrays - Time complexity of unshift() vs. push() in Javascript - Stack Overflow
If anyone is curious, using push with shift is faster than unshift with pop. 2018-03-03T04:03:17.157Z+00:00 ... @douggard that's interesting, I measured the opposite with shift being 15x slower than unshift. Why is unshift so much faster than shift? 2024-12-13T18:29:55.22Z+00:00 ... 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... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a resource to find the time complexity for common methods
Blog Array iteration methods are going to be O(n) if they iterate through 1x, e.g.: forEach map reduce entries find some every Or if they convert the entire array or a subset of the array to a new form slice splice spread/rest Or O(n) if they require that the entire array be recalculated shift unshift Or O(1) if they directly access a property arr[index] Or O(1) if they modify the last element in the array push pop Or O(nlogn) if they have to make multiple passes through the array, but each pass decreases in size sort Once you understand Space/Time complexities and have a rough idea of what is happening under the hood, it's pretty easy to just guess. More on reddit.com
๐ŸŒ r/learnjavascript
8
1
November 1, 2022
Time complexity of shift/unshift

Seems like that would be dependent on the specific engine's implementation.

More on reddit.com
๐ŸŒ r/learnjavascript
7
1
February 9, 2020
Big O of JavaScript arrays - Stack Overflow
It somewhat masks the fact that most languages arrays are fixed-size, and require complex operations to resize. It seems that JavaScript makes it easy to write poorly performing array code. This leads to the question: What performance (in terms of big O time complexity) can I expect from JavaScript ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 - In other words, it determines an algorithmโ€™s worst-case time complexity. The maximum runtime of an algorithm is expressed using the Big O Notation in data structures. One or more elements are added to the end of an array using the push() method, which also returns the arrayโ€™s new length This technique modifies the arrayโ€™s length.
๐ŸŒ
DEV Community
dev.to โ€บ technoph1le โ€บ the-javascript-arraypush-method-explained-5d4m
The JavaScript `Array.push()` method explained - DEV Community
November 24, 2022 - Since the push() method adds elements to the end of an array, it has a constant time complexity of O(1).
๐ŸŒ
DEV Community
dev.to โ€บ lukocastillo โ€บ time-complexity-big-0-for-javascript-array-methods-and-examples-mlg
Time complexity Big 0 for Javascript Array methods and examples. - DEV Community
June 3, 2020 - Hello everyone, some weeks ago ... using JavaScript as the programming language, and normally after finished to implement an algorithm I like to calculated complexity with the Big 0 notation. That is the reason why I wanted to write this post, to understand the time complexity for the most used JS Array methods. So, let's start with a quick definition of the method, his time complexity, and a small example. 1. push() - 0(1) Add ...
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/

๐ŸŒ
Witch
witch.work โ€บ en โ€บ posts โ€บ javascript-array-insert-time-complexity
Exploring JavaScript - Time Complexity of Array Insertion Methods
February 22, 2024 - By ignoring error checking and focusing on the logic, it repeatedly adds the received arguments to the end of the array and increments the array length by 1 for each argument. Therefore, the time complexity can be considered O(number of arguments), ...
๐ŸŒ
Hey
world.hey.com โ€บ mgmarlow โ€บ time-complexity-of-array-push-d950f9dc
Time complexity of Array.push
April 3, 2021 - After a new array is allocated, the existing array contents are copied into it. In terms of time complexity, push is O(1) if the array has room to fit the new element and O(n) if it needs to allocate more space.
Find elsewhere
๐ŸŒ
Mblogs
mbloging.com โ€บ home โ€บ dsa โ€บ understanding time complexity of javascript array operations
Understanding Time Complexity of JavaScript Array Operations | Mbloging
April 15, 2025 - Accessing elements in an array by index is considered a constant time operation (O(1)). Regardless of the array's size, accessing an element at a specific index takes the same amount of time because arrays provide direct access to memory locations ...
๐ŸŒ
CodingNomads
codingnomads.com โ€บ javascript-array-unshift-shift-pop-push
JavaScript Array Essentials: Using pop, push, shift, and unshift
This is because each element in the array must be moved to a new position when an item is added to or removed from the beginning of the array. On the other hand, .pop() and .push() have a time complexity of O(1), which means their execution ...
๐ŸŒ
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 - Because it takes a single step to access an item of an array via its index, or add/remove an item at the end of an array, the complexity for accessing, pushing or popping a value in an array is O(1).
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ javascript array push() method
JavaScript Array push() Method - Scaler Topics
June 8, 2023 - Whenever we invoke the push method on an array it just adds the passed value in the parameter at the end of that array. This means the length of an array doesn't matter. In this case: Time complexity will be O(1). ... To add elements to a JavaScript ...
๐ŸŒ
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 - There is no specified time complexity guarantee for any array operation. How arrays perform depends on the underlying data structure the engine chooses. Engines might also have different representations, and switch between them depending on certain heuristics.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ time complexity of shift/unshift
r/learnjavascript on Reddit: Time complexity of shift/unshift
February 9, 2020 -

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?

๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ data types
Arrays
The algorithm requires exactly 1 array pass, so the time complexity is O(n).
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Time Complexity Practice 1 in JavaScript | AlgoCademy
This function creates a new array populated with the results of calling a provided function on every element in the calling array. The time complexity is O(n) because it iterates over each element of the array.
Top answer
1 of 2
135

NOTE: While this answer was correct in 2012, engines use very different internal representations for both objects and arrays today. This answer may or may not be true.

In contrast to most languages, which implement arrays with, well, arrays, in Javascript Arrays are objects, and values are stored in a hashtable, just like regular object values. As such:

  • Access - O(1)
  • Appending - Amortized O(1) (sometimes resizing the hashtable is required; usually only insertion is required)
  • Prepending - O(n) via unshift, since it requires reassigning all the indexes
  • Insertion - Amortized O(1) if the value does not exist. O(n) if you want to shift existing values (Eg, using splice).
  • Deletion - Amortized O(1) to remove a value, O(n) if you want to reassign indices via splice.
  • Swapping - O(1)

In general, setting or unsetting any key in a dict is amortized O(1), and the same goes for arrays, regardless of what the index is. Any operation that requires renumbering existing values is O(n) simply because you have to update all the affected values.

2 of 2
5

guarantee

There is no specified time complexity guarantee for any array operation. How arrays perform depends on the underlying datastructure the engine chooses. Engines might also have different representations, and switch between them depending on certain heuristics. The initial array size might or might not be such an heuristic.

reality

For example, V8 uses (as of today) both hashtables and array lists to represent arrays. It also has various different representations for objects, so arrays and objects cannot be compared. Therefore array access is always better than O(n), and might even be as fast as a C++ array access. Appending is O(1), unless you reach the size of the datastructure and it has to be scaled (which is O(n)). Prepending is worse. Deletion can be even worse if you do something like delete array[index] (don't!), as that might force the engine to change its representation.

advice

Use arrays for numeric datastructures. That's what they are meant for. That's what engines will optimize them for. Avoid sparse arrays (or if you have to, expect worse performance). Avoid arrays with mixed datatypes (as that makes internal representations more complex).

If you really want to optimize for a certain engine (and version), check its sourcecode for the absolute answer.

๐ŸŒ
Quora
quora.com โ€บ What-is-the-time-complexity-of-the-push-and-pop-operation-of-an-array-based-stack
What is the time complexity of the push and pop operation of an array-based stack? - Quora
Answer (1 of 7): Push : Every time a new element is added at the end of array. So, irrespective of number of elements already in the array, this operation will always take same time. So it will be O(1). Pop : Every time first element of array is removed, all remaining n-1 elements are moved up. ...