JavaScript’s Push, Pop, Shift, and Unshift Array Methods With Big O Notation.

Omkesh B. Kendre
4 min readNov 28, 2022

--

Big O Notation:

Big O Notation is a technique for expressing how time-consuming an algorithm is. As the input increases, it calculates how long it will take to perform an algorithm. 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.

1. push() : Array.prototype.push()

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.

Time Complexity of push() is O(1)

A new element is added to the stack and the top is modified to point to the new element when the function is invoked. A connection is also established between the new and previous top pointers. These operations are of a constant time.

const alphabets = ['A', 'B', 'C'];
console.log(alphabets); // ['A', 'B', 'C']
const count = alphabets.push('D');
console.log(alphabets); // ['A', 'B', 'C', 'D']
console.log(count); // 4

2. pop() : Array.prototype.pop()

The last element in an array is removed by the pop() method, which then returns the removed element. This technique modifies the array’s length.

Time Complexity of pop() is O(1)

The top element is deleted with this process, and the pointer that was pointing at the top-most element is now pointing at the element immediately behind it. All of the operations in this scenario are carried out in constant time.

const alphabets = ['A', 'B', 'C', 'D'];
const removeedAlphabets = alphabets.pop()
console.log(alphabets); //['A', 'B', 'C']
console.log(removeedAlphabets); // D

3. shift(): Array.prototype.shift()

The first element in an array is deleted by the shift() method, which then returns the removed element. This technique modifies the array’s length.

Time Complexity of shift() is O(n)

The first element is deleted with this process, and the pointer that was pointing at the first element is now pointing at the element immediately after it. All of the operations in this scenario are carried out n time because it will shift all pointers from the first element to the last element.

const alphabets = ['A', 'B', 'c', 'D'];
const firstAlphabet = alphabets.shift();
console.log(alphabets); //['B', 'c', 'D']
console.log(firstAlphabet); // A

4. unshift() : Array.prototype.unshift()

One or more elements are added to the First of an array using the unshift() method, which also returns the array’s new length This technique modifies the array’s length.

Time Complexity of unshift() is O(n)

A new element is added to the stack and the bottom is modified to point to the new element when the function is invoked. A connection is also established between the new and previous top pointers. All of the operations in this scenario are carried out n time because it will shift all pointers from the first element to the last element.

const alphabets = ['B', 'C', 'D'];
const count = alphabets.unshift('A');
console.log(alphabets); //['A', 'B', 'C', 'D']
console.log(count); //4

Analysis of Big O

The sample programming shown below will help you to understand the Big O complexities of the push(), pop(), shift(), and unshift() functions.

let finalArray = [];
finalArray.length = 10000000;
var time1 = performance.now();
finalArray.push(1)
var time2 = performance.now();

console.log(`Time calculated push(): ${(time2 - time1) / 1000} seconds.`)

var time3 = performance.now();
finalArray.unshift(1)
var time4 = performance.now();

console.log(`Time calculated unshift(): ${(time4 - time3) / 1000} seconds.`)

var time5 = performance.now();
finalArray.pop()
var time6 = performance.now();

console.log(`Time calculated pop(): ${(time6 - time5) / 1000} seconds.`)

var time7 = performance.now();
finalArray.shift()
var time8 = performance.now();

console.log(`Time calculated shift(): ${(time8 - time7) / 1000} seconds.`)


/**
Result:
Time calculated push(): 0.012700000047683716 seconds.
Time calculated unshift(): 0.5321000000238418 seconds.
Time calculated pop(): 0 seconds.
Time calculated shift(): 0.5927999999523162 seconds.
**/

The program above includes a list of the statistics that were utilized to calculate performance. (Note: Each time the same application is run, the number will alter. Consider running it multiple times and examining the statistical results.)

Happy Codding ….. :)

--

--