4

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
  • 171
  • 1
  • 13

3 Answers3

12

"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
  • 191,408
  • 23
  • 240
  • 301
4

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
  • 732,580
  • 175
  • 1,330
  • 1,459
Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
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
  • 516
  • 4
  • 8