The Big "O" of JavaScript Object Explained

The Big "O" of JavaScript Object Explained

This article will cover what a javascript object is, the big "O" notation, and some basic example/illustration that describes both javascript objects and the big "O" notation. Although a couple of terms will be explained it is also very important that you have an understanding of what JS objects are and what they can do.

Javascript Object

JavaScript(JS for short) objects are actually complex datatype that allows the storage of variables in a key-value pair.

const obj = {name:"solomon",age:21,isMale:true,favNumber:[2,4,6,8]}
//keys = name, value = "solomon"

usage

The most efficient times to use an object in JS is when

  1. You don't need an order for your data

  2. You need fast insertion or removal of data

The big 😮!!!

Not referring to the letter "O" present in the word "Object" but the "Big O" notation describes the performance of an algorithm or data structure in terms of how the number of operations it performs grows with the size of the input.

In the case of JavaScript objects, the Big O notation can be used to describe the performance of various operations on the object and their methods, such as insertion, removal, search for properties etc.

Insertion

Adding a new property to a JavaScript object has a constant time complexity, or O(1). This means that the number of operations required to add a new property to an object does not depend on the number of properties already in the object making inserting new object properties independent and fast.

const person = {name: "smiles",age:20};
person.favColor = "bala blu"; //does not depend on the person object
console.log(person); // returns {name: "smile",age:20, favColor: "bala blu"}

Removal

Removing a property from a JavaScript object has a constant time complexity, O(1). This means that the number of operations required to remove a property from an object does not depend on the number of properties already in the object making removal faster.

const obj = {name:"solomon",age:21,isMale:true,favNumber:[2,4,6,8]}
delete obj.favNumber; // not depending on the obj 
console.log(obj) // returns{name:"solomon",age:21,isMale:true}

Access

Accessing a property in a JavaScript object has a constant time complexity, O(1), as well. This means that the number of operations required to retrieve a property's value does not depend on the number of properties in the object.

const person = {name: "smiles",age:20};
console.log(person.name);//retuns "smiles"

Searching for a specific property in a JavaScript object has an average time complexity of O(n) and worst-case complexity of O(n) in an object where the properties are unordered, meaning due to the unorganized nature of an object the speed of searching in an object depends on the object size

Object.keys(), Object.values(), Object.entries()

These method has a linear time complexity, O(n), where n is the number of properties in the object. This means that number of operations is relative to the length of the object in other words, the number of operations required to enumerate the object's properties increases linearly with the number of properties in the object.

const obj = {name:"solomon",age:21,isMale:true};
console.log(Object.keys(obj)) // returns ["name","age","isMale"]
console.log(Object.values(obj)) // returns ["solomon",21,true]
console.log(Object.entries(obj)) // returns [["name","solomon"],["age",21],["isMale",true]]

Object.hasOwnProperties()

This method has a complexity of O(1), meaning it also has a constant time complexity and what it does is check if a certain property exist in an object and return a boolean value(true/false) depending on the result.

const obj = {name:"solomon",age:21,isMale:true,favNumber:[2,4,6,8]};
console.log(Object.hasOwnProperty("name")) // returns true

Conclusion

The most common operations on JavaScript objects such as insertion, access, and removal have a constant time complexity, which means they are very fast regardless of the size of the object.

However,Object.keys(),Object.values(),object.entries() and searching have linear time complexity, which means they will become slower as the size of the object increases.

Found this useful? Share, like and follow for more contents like this thanks😊.