I’m new to data structure and I’m learning it in Javascript.
My Question is: Why do we need hash tables when we 've objects in javascript? Can anybody give me a situation where hash tables will be more useful than objects?
I’m new to data structure and I’m learning it in Javascript.
My Question is: Why do we need hash tables when we 've objects in javascript? Can anybody give me a situation where hash tables will be more useful than objects?
"Hashtable" is called different things in different languages. Java has Hashtable
and HashMap
, Ruby has Hash
, Python has dict
... in JavaScript, it's called Map
.
Objects' keys are limited to strings; Map
keys can be anything.
Objects support inheritance; a Map
only contains what is specifically put into it.
Think you means Map instead of HashTable. IMHO Map may be more useful and perform better if you need one of that:
I think you can obtain more information at MDN
The MDN docs on this are quite helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Objects_and_maps_compared
Most notably, using a map gives you the advantage of using anything as a key, maps retain order, and may perform better when constantly adding and removing values.