DEV Community

Martins Olumide
Martins Olumide

Posted on

Time Complexity of JavaScript Array Methods (Part One)

JavaScript array methods are simply function (also called method) that perform specific task and computations on the array it is called on. Majority of the time, they either return a new array, mutate an existing array, search an array, or return some truthy or falsely value based on a condition or statement. Knowing the time complexity of the array methods being used is not only eye-opening, it also allow enhance the performance of our programs and applications.

So, lets dive in my JavaScript Geeks ๐Ÿ˜Ž๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป;

  1. Array.pop(): This array method is the type that returns an item (generically called element) from an array, pop() removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified. let's see an example.
let names = ['Dave', 'Martins', 'Tobby', 'Rielle']
names.pop()
//return 'Rielle' since it is the last element of the array

let emptyNames = []
names.pop()
//returns undefined since there are no element in the array
Enter fullscreen mode Exit fullscreen mode

from the example above, think about the names in the array as people on a queue, since pop() return the last element, the last person on the queue will be removed and returned. And doing this simply have no effect on the rest of the people since there is no need for reindexing of rest of the people in the queue. This implies that the time complexity to remove an element in an array is O(1).

2..Array.push():
push() is an array method that appends a new elements to the end of an array, and returns the new length of the array. let's proceed using our names array;

names = ['Dave', 'Martins', 'Tobby']
//This is our new array since 'Rielle' has been removed
names.push("Wick")
console.log(names) // ['Dave', 'Martins', 'Tobby', 'Wick']
Enter fullscreen mode Exit fullscreen mode

push() is performing the opposite operation of the pop() method. pop() removes, push() adds. So, what do you think the Time complexity to add to people on the queue will be? my answer is O(1). How I got my answer?, well adding a new person to that names array doesn't require the change of the indexes of the persons in the array therefore, the only person changing index is the new person being added.

3..Array.Unshift():
unshift() is an array method that adds a new element to the start of an array i.e array index 0, It is working in same way the array.push() is but different postion.

names = ['Dave', 'Martins', 'Tobby', 'Wick']
names.unshift("Maroon")
console.log(names) // ['Maroon', 'Dave', 'Martins', 'Tobby', 'Wick']
Enter fullscreen mode Exit fullscreen mode

the above example shows that Maroon has taken the postion of index 0, which was the initial position of Dave. This signifies that Dave's index is now 1 and same is applicable to the rest of the elements in the array

//initial indexing
['Dave', 'Martins', 'Tobby', 'Wick']
Dave = 0
Martins = 1
Tobby = 2
Wick = 3

//latest indexing since calling the unshift method
['Maroon', 'Dave', 'Martins', 'Tobby', 'Wick']
Maroon = 0
Dave = 1
Martins = 2
Tobby = 3
Wick = 4
Enter fullscreen mode Exit fullscreen mode

The time complexity for this operation is O(n).

4..Array.shift():
The shift() method of Array instances removes the first element from an array and returns that removed element. This method changes the length of the array.

names = ['Maroon', 'Dave', 'Martins', 'Tobby', 'Wick']
names.shift() // returns Maroon
//new array state
['Dave', 'Martins', 'Tobby', 'Wick']
Enter fullscreen mode Exit fullscreen mode

For this also, reindexing would have to occur, consequently, the time complexity will be O(n) too.

5..Array.forEach():
The forEach() method of Array instances executes a provided function once for each array element. It takes a callback function with a couple of parameters. Basically, it loops through each element in the array and the time complexity for this operation is O(n) as the time taken will be proportional to the number of input.

Conclusion
This is concludes the first part of the Time Complexity series, where we discuss thoroughly JavaScript methods and their time complexity. Stay tuned for the next parts I'll be uploading, have a nice day ๐Ÿ™‚โœŒ๏ธ

Top comments (2)

Collapse
 
abc_wendsss profile image
Wendy Wong

Hi Martins, Thank you for sharing tips on javascript methods and publishing your first article on DEV! Welcome to the DEV Community ! ๐ŸŒ

Collapse
 
martinsolumide8 profile image
Martins Olumide

๐Ÿ‘๐Ÿฝ