In the previous two posts, I talked about Time and Space complexity using functions as an example. As for this post, I'll just share what I've learned about JavaScript objects. Retrieving and inserting information on a JavaScript object is very fast as it is done in constant time:

  • Access - O(1)
  • Insertion - O(1)
  • Deletion - O(1)
  • However, Searching is O(n)

In searching, we mean searching the object for a particular value. We would have to go through the whole object, checking each key if they have the value that we would want. So, the larger the object, the longer it would take to search for the particular value.

With all of that clear, let's talk about the object helper methods that is built-in to JavaScript:

  • Object.keys - O(n)
  • Object.values - O(n)
  • Object.entries - O(n)
  • hasOwnProperty - O(1)

The reason why the first 3 helper methods are O(n) is due to the fact that those methods would return an array, where the array's size is dependent on the size of the object. So, the larger the object, the longer it would take.

hasOwnProperty is similar to accessing an object, which is constant time (`O(1)`)