DEV Community

Cover image for The Big O of Objects
Arya Krishna
Arya Krishna

Posted on

The Big O of Objects

Let's start with understanding what is an object.

An object is a collection of properties, and a property is an association between a name (or key) and a value.

In real Life a car is an Object with properties like model, color etc. Objects are variables with a bunch of values assigned to them. It is a common practice to declare objects with the const keyword.

Let's see an example of an Object

const student = {firstName:"Arya", lastName:"Krishna", language:Javascript};
Enter fullscreen mode Exit fullscreen mode

In short objects are unordered data structures and everything is stored in key value pairs.

In the example above the variable student has 3 key value pairs.

When should you be using Objects in your code?
Objects works well when you don't need a specific order to work with and also when you want fast access and insertion and removal.

Insertion, Removal and Access are constant time in Objects. Searching however is linear time. Here searching doesn't means looking for a key, that comes under access and we have already mentioned it as constant time. Searching here is checking to see if a given piece of information is a value somewhere. As the object grows the number of operations also keep on increasing.

Object.keys = O(n)
Object.values = O(n)
Object.entries = O(n)
hasOwnProperty :O(1)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)