5

How is a Map different from a dictionary/object?

In other words, what is the difference between let x = {} and let x = new Map() ?

Jay
  • 8,544
  • 6
  • 31
  • 38
  • 3
    "The Map object is a simple key/value map. **Any value (both objects and primitive values) may be used as either a key or a value**." -- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map – elclanrs Nov 15 '15 at 23:44
  • 1
    That page from Mozilla also has a section "objects and Maps compared". – Thilo Nov 15 '15 at 23:45
  • 1
    The biggest difference is that a `Map` can use any value as a key (such as another Object) where as using `{}` requires a string as the key. In fact, a Map can contain separate items with keys of `"1"` and `1`, but a plain object cannot. And, of course, a Map object has built in methods that apply to a Map object that `{}` does not have. – jfriend00 Nov 15 '15 at 23:59
  • Another thing to keep in mind: a Map instance *is also an object*, and can have properties etc. just like any other object. You can create a Map instance and give the object named function methods for example. – Pointy Nov 16 '15 at 00:08
  • I don't understand the constant references to MDN. The [*language specification*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-map-objects) is pretty clear (on this at least) and has the benefit if being the definitive reference. – RobG Nov 16 '15 at 00:29

1 Answers1

9

Objects and maps compared (from MDN):

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Because of this (and because there were no built-in alternatives), Objects have been used as Maps historically; however, there are important differences between Objects and Maps that make using a Map better:

  • An Object has a prototype, so there are default keys in the map. This could be bypassed by using map = Object.create(null) since ES5, but was seldomly done.
  • The keys of an Object are Strings and Symbols, where they can be any value for a Map.
  • You can get the size of a Map easily while you have to manually keep track of size for an Object.

This does not mean you should use Maps everywhere, objects still are used in most cases. Map instances are only useful for collections, and you should consider adapting your code where you have previously used objects for such. Objects shall be used as records, with fields and methods. If you're still not sure which one to use, ask yourself the following questions:

  • Are keys usually unknown until run time, do you need to look them up dynamically?
  • Do all values have the same type, and can be used interchangeably?
  • Do you need keys that aren't strings?
  • Are key-value pairs often added or removed?
  • Do you have an arbitrary (easily changing) amount of key-value pairs?
  • Is the collection iterated?

Those all are signs that you want a Map for a collection. If in contrast you have a fixed amount of keys, operate on them individually, and distinguish between their usage, then you want an object.

alainschaller
  • 89
  • 2
  • 9
Thilo
  • 241,635
  • 91
  • 474
  • 626
  • Another thing that may be of significant importance is that the Map contents are ignored by `JSON.stringify()` – Pointy Nov 16 '15 at 00:13
  • 1
    Please make sure to format your quotation of my MDN text as a quote :-) – Bergi Nov 16 '15 at 00:20