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.

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/

Discussions

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
Big O of JavaScript arrays - Stack Overflow
Arrays in JavaScript are very easy to modify by adding and removing items. It somewhat masks the fact that most languages arrays are fixed-size, and require complex operations to resize. It seems t... More on stackoverflow.com
🌐 stackoverflow.com
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
How do you find time complexity when using Javascript array methods
I believe this one is effectively quadratic. Or to be specific, O(mn), where m = arr.length and n = arr2.length. Filter is linear if its callback is constant. But in this case the callback is also linear. if the arrays increase in length the time would also increase. You can only deduce that it's not constant from this. It doesn't tell you whether it's linear, logarithmic, exponential, quadratic, etc. If arr2 is sorted, you could improve the runtime by using a binary search on it. If both arrays are sorted, I'm pretty sure you could make this effectively linear, though I'm not sure how you'd write that functionally. More on reddit.com
🌐 r/webdev
8
1
July 22, 2023
🌐
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
For fixed size array, the time complexity is O(1) for both the push and pop operations as you only have to move the last pointer left or right.
🌐
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.
🌐
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), ...
🌐
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 - 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 a new element to the end of the array.
🌐
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
🌐
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.
🌐
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).
🌐
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 ...
🌐
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).
🌐
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 - Time Complexity — O(n) This method linearly iterates through the array to see if at least 1 element matches the test. In the worst case it has to check all the elements. Space Complexity — O(1) The method returns a boolean value. The push() method adds one or more elements to the end of ...
🌐
Quora
quora.com › What-is-algorithmic-complexity-of-push_back-in-std-vector-Assume-default-allocator
What is algorithmic complexity of push_back in std::vector? (Assume default allocator) - Quora
Most of the time push_back just ... cost of O(arr.size()). Despite having a worse case scenario of O(N) push_back, most of the time it is O(1). The average push_back, I.e., amortized complexity is guaranteed to be ...
🌐
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 - Its position is updated to zoo[4] and then all the other animals’ position gets updated as long as the array is, so the complexity will be O(n). The purpose of this article was to familiarize ourselves with under-the-hood Javascript array methods regarding time complexity.
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.

🌐
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?

🌐
AlgoCademy
algocademy.com › link
Time Complexity Practice 1 in JavaScript | AlgoCademy
The time complexity is O(1) because it only involves adding an element to the end of the array.