Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading