Big O Notation
By: Matthew Sedlacek
Tags: Big O Notation, Software Engineering, Arrays, Algorithms, Data Science

Big O Notation

Arrays and their Built-in Methods

Photo by Oliver Schwendener on Unsplash

In my previous Big O Notation blogs, we discussed Time and Space Complexity and Objects. Now we are going to look at Big O and how it relates to arrays and their built-in methods.

An array is an ordered data structure and can be used to store data of any type. Or, in more technical terms, “When we initialize an array in a programming language, the language allocates space in memory for your array, and then points that starting variable to that address in memory. Then the program assigns a fixed amount of memory for each element” (Learn). Below is a simple example of an array with multiple data types.

const array = [ "a", {}, 7, [], false]

In terms of Big O, arrays are favorable when we need fast access to elements. In the example above, we can access each element by its index; array[0] = “a” and array[4] = false. Since the elements are indexed, our computers know exactly where the element is and can go directly to the element. Moreover, an array with 2 elements or 2,000 elements has the same time complexity, O(1), for accessing methods.

Arrays can also be fast in some insertion or removal methods. These types of methods have either an O(1) or O(n). The difference comes from where in the array elements are inserted or removed. If elements are inserted or removed from the end of an array, these methods have an O(1) because just the last indexed element is impacted. However, if elements are inserted or removed from the beginning of an array, all elements are impacted. For example, if we add an element to the beginning of the array, the added element takes over index 0, and the element that previously was in index 0 is now at index 1 and so forth. Since the number of operations grows with the number of elements in the array, these methods have an O(n).

Lastly, Arrays have an O(n) for searching methods. This is because the worst case scenario for a search method is checking every element in the array.

Array Built-in Methods

Arrays come with many different built-in methods, but the methods we will look at here are push, pop, shift, unshift, concat, slice, splice, sort, and map.

The first two methods push and pop involve adding or removing an element from the end of the array. As we mentioned earlier, these types of methods have an O(1) because they only impact the array’s last element.

Shift and unshift, involve adding or removing from the beginning of the array. As explained earlier, the run-time of these methods grow with the inputs or O(n) because they impact all indexes in the array.

Similar to shift and unshift, concat, slice, and splice have an O(n). Concat is used to combine two or more arrays into a new array. Moreover, as the number of inputs in each array that you are combining grows, so does the run-time in a linear fashion. Slice involves making a copy of an array and, also, has a run-time that grows in a linear fashion with inputs. Splice is a method that, “changes the contents of an array by removing the existing elements and/or adding new elements” (MDN). Although splice can remove/add elements from anywhere in the array it has an O(n) because, in the worst case scenario, all array inputs would be impacted.

Sorting methods have an O(n * log n) which is worse than O(n). This is because sorting methods require comparisons between the elements and elements may need to be visited more than once.

The last built-in method we will discuss for arrays is map. This method has an O(n) and works by creating, “…a new array populated with the results of calling a provided function on every element in the calling array” (MDN). The reason that this function has an O(n) is due to fact that it must interact with every element in the array at least once. So, the size of the array directly impacts the run-time.

See below for a summary of the method types and built-in methods discussed in this blog and how they relate to Big O Notation.

Summary Table based on lecture material presented in Colt Steele’s Udemy Course

Thank you for taking the time to learn more about arrays through the lens of Big O notation. This is the last blog in my Big O notation series, I hope you now understand the importation of Big O notation and can apply it as you write and interpret code.

Resources

Steele, C. (n.d.). JavaScript Algorithms and Data Structures Masterclass. Online Course.

Arrays Underneath. (n.d.). Retrieved October 30, 2020, from https://learn.co/lessons/arrays-underneath

“Array.prototype.splice().” MDN Web Docs, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice.

“Array.prototype.map().” MDN Web Docs, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map.

Gain Access to Expert View — Subscribe to DDI Intel