TIME COMPLEXITY (objects && Arrays)

Ewuzie Joan
5 min readOct 14, 2023

--

Data structures, at a high level, are techniques for storing and organizing data that make it easier to modify, navigate, and access. Data structures determine how data is collected, the functions we can use to access it, and the relationships between data. JavaScript freely gave us two data structures which are Objects and Arrays, before other data structures that is created by the developers themselves like the Linklist, Queue, Stack, Table etc.

Focusing on Arrays and Objects and trying to find out the time complexity for each of its methods. I’d be talking about the Array.

1. Array

An array is a non-primitive data type & | structure used to store values and other datatypes including itself. It is identified with a square bracket ‘[ ]’. It is the most basic of all data structures. An array stores data in memory for later use. Each array has a fixed number of cells decided on its creation, and each cell has a corresponding numeric index used to select its data. Whenever you’d like to use the array, all you need are the desired indices, and you can access any of the data within.

Image depicting the array in the CPUand it’s number of created cells
Image depicting an array in the CPU;s memory and its created cells. Picture credit to Amanda Fawcett

It is simple to use and it is the foundational building block for complex data structures. But everything has it’s pros and cons right?. The downside of the array is its fixed side, and the expensiveness to insert, remove or resequence values.

Which leads us to Array method and their time complexity and also when to use an Array<[]>. Looking at some of the common array methods and the higher array methods i’d be explaining on this page with some images to help stimulate and make the brain not forget it.;

Array length
Array pop()
Array push()
Array shift()
Array unshift()
Array join()
Array concat()
Array flat()

Array map()
Array splice()
ray slice()

THE SHIFT( )

  • The Shift array methods removes the first element from an array and returns it. If the array is empty…. Undefined is returned and the array is not modified.

*Unshift array method: While the shift method removes the first element of an array….the unshift method adds a new element at the start of the array and returns the new length of the array.

*The Pop array method: The pop method works exactly like the shift method except it removes the last element from an array while the shift method removes the start element. Same rule…. undefined is returned if the array is empty and it is not modified.

*The Push array method: it adds new elements to the end of the array and returns the new length of the array.

*The Join array method is a string used to separate one element of an array from the next in the resulting string with the specified separator string. If omitted, the array elements are separated with a comma.

*The Slice method is the index to the beginning of a specified portion of your string. If a second value is not inputted in the parameter, the substring continues to the end, but inputting a second index tells your…..computer where to stop slicing.

*The Split method is a string that identifies a character or characters to use in se*The Shift array methods removes the first element from an array and returns it. If the array is empty…. Undefined is returned and the array is not modified.

*Unshift array method: While the shift method removes the first element of an array….the unshift method adds a new element at the start of the array and returns the new length of the array.

*The Pop array method: The pop method works exactly like the shift method except it removes the last element from an array while the shift method removes the start element. Same rule…. undefined is returned if the array is empty and it is not modified.

*The Push array method: it adds new elements to the end of the array and returns the new length of the array.

*The Join array method is a string used to separate one element of an array from the next in the resulting string with the specified separator string. If omitted, the array elements are separated with a comma.

*The Slice method is the index to the beginning of a specified portion of your string. If a second value is not inputted in the parameter, the substring continues to the end, but inputting a second index tells your…..computer where to stop slicing.

*The Split method is a string that identifies a character or characters to use in se*The Shift array methods removes the first element from an array and returns it. If the array is empty…. Undefined is returned and the array is not modified.

*Unshift array method: While the shift method removes the first element of an array….the unshift method adds a new element at the start of the array and returns the new length of the array.

*The Pop array method: The pop method works exactly like the shift method except it removes the last element from an array while the shift method removes the start element. Same rule…. undefined is returned if the array is empty and it is not modified.

*The Push array method: it adds new elements to the end of the array and returns the new length of the array.

*The Join array method is a string used to separate one element of an array from the next in the resulting string with the specified separator string. If omitted, the array elements are separated with a comma.

*The Slice method is the index to the beginning of a specified portion of your string. If a second value is not inputted in the parameter, the substring continues to the end, but inputting a second index tells your…..computer where to stop slicing.

*The Split method is a string that identifies a character or characters to use in se*The Shift array methods removes the first element from an array and returns it. If the array is empty…. Undefined is returned and the array is not modified.

*Unshift array method: While the shift method removes the first element of an array….the unshift method adds a new element at the start of the array and returns the new length of the array.

*The Pop array method: The pop method works exactly like the shift method except it removes the last element from an array while the shift method removes the start element. Same rule…. undefined is returned if the array is empty and it is not modified.

*The Push array method: it adds new elements to the end of the array and returns the new length of the array.

*The Join array method is a string used to separate one element of an array from the next in the resulting string with the specified separator string. If omitted, the array elements are separated with a comma.

*The Slice method is the index to the beginning of a specified portion of your string. If a second value is not inputted in the parameter, the substring continues to the end, but inputting a second index tells your…..computer where to stop slicing.

*The Split method is a string that identifies a character or characters to use in separating the string. In a simple definition……it divided a string into substrings using the specified separator and returns them as an array. If omitted, a single array containing the entire string is returned .

--

--

Ewuzie Joan

A Frontend Developer and a Technical and Creative writer.