2

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?

Zullu
  • 143
  • 12

3 Answers3

8

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

Amadan
  • 179,482
  • 20
  • 216
  • 275
3

Think you means Map instead of HashTable. IMHO Map may be more useful and perform better if you need one of that:

  • keep order of insertions of key/value pairs;
  • frequent additional and removal;
  • key which not String/Symbol.

I think you can obtain more information at MDN

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Vasyl Moskalov
  • 3,627
  • 1
  • 21
  • 26
0

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.

Jan-Luca Klees
  • 486
  • 3
  • 8