What is the time complexity of removing/accessing property in an object in javascript

I was watching a youtube tutorial in DS&A (using JS) and he mentioned that the time complexity of accessing or removing property in an object is constant?

how is that possible shouldn’t we search for this property first and then delete it or access it. so, it should be linear!

>Solution :

JavaScript objects are actually hash tables under the hood. (See https://en.wikipedia.org/wiki/Hash_table for more info on how hash tables work.)

In simple terms, in a hash table, the key itself directly correlates to a number when put through the hash table’s hashing function, which number is (or correlates to) an address. Thus, JavaScript can (typically) directly use the key name to access that number and/or address. This has runtime complexity O(1) because the key’s string (if it’s a string) can be decoded into an address without any iteration over the other keys in the object.

Leave a Reply