ES6 Map vs WeakMap vs plain Objects – Describing the differences

The version 6 of EcmaScript has introduced a lot of things to JavaScript and one of the most important one is Map, which is a very popular data structure in other programming languages. Though the plain old JavaScript objects looks pretty similar to what map is; but the actual Map has been introduced in ES6 only.


What is Map?

In simple words, map means relating one thing to another. That one thing is called key and the another is value. Both key and value can be of any type. The major difference of Map from JavaScript Object is; objects accepts only String or Symbol key, but Map accepts anything. Below is a simple example of how to use ES6 Map.

Map examples

Hope the examples above has cleared your confusion regarding the differences between Map and Object to a certain extent. But the differences are not limited to getters, setters or type of keys. More differences are given below.

ES6 Map vs Object

Object Map
Objects were there since the birth of JS. Map is introduces in ES6.
The keys can only be String or Symbol. Accepts anything (even NaN) as key.
Assign values with = operator. .set() and .get() functions are used.
Not an iterable. You can not use myObj.forEach() etc. Is an iterable and forEach for of can be used.
You can not get size of an object easily. .size property of a map shows the size.
As objects has prototypes, key conflicts may occur. Map is a map. You can even do myMap.set("set", "will not change set").
To clear properties, you need to write manual code. Map has .clear() to clear everything.
If all keys are strings, and you just use get and set, Objects are more performant. If you focus on iterating, map will be more performant. (TEST)

You can learn more about Map in MDN


What is a WeakMap

WeakMap was also introduced by ES6 in 2015. By the name we can guess that it’s some special kind of Map may be; but actually it’s not. WeakMap is not inherited from Map. However their functionalities are very much similar.

The differences which Map has with Objects; are also true (majorly) for WeakMap. The major difference between Map and WeakMap is, WeakMap holds the references of the key objects weakly. Thus if that object is deleted somewhere in the program, WeakMap will also release the value mapped to that object. This prevents memory leak. This is the reason WeakMap is used to declare private variables. However there are more differenced between Map & WeakMap.

Map vs WeakMap

Map WeakMap
Key in Map can be anything. Key in WeakMap must be of type Object (not null).
Map has .size WeakMap has .length
.forEach() is available. No .foreach() here.
Nothing is auto destroyed. If a key object is deleted, the value assigned against that key in any WeakMap will also be destroyed.

Below are some examples of WeakMaps. To know more about WeakMaps, you can check MDN document.

WeakMap examples

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.

  • in the ES6 Map vs Object the headers are wrong

    • Paul Shan

      Oh.. big mistake. Thanks man 🙂

  • Silicum Silium

    I did not understand the main difference.

    • Paul Shan

      WeakMap releases the object if no more reference of that is available.

    • Silicum Silium

      Thank you.

  • Mihai Cicioc

    The reason why WeakMap is used for private members is not because WeakMap releases resources aka garbage collection… That release is a nice feature, and is a consequence for “WeakMap accepts only objects as keys”.

    The reason for private members is the consequence of “WeakMap does not have iterator methods”.

    In order to access a member on an object, you would need to perform some kind of introspection on that object (find out what capabilities that object exposes, like using Object.keys(someObject), Object.entries(…), …).

    But because WeakMap does not have those functions (keys(), entries(), …), then you can’t really know the capabilities (from outside), unless you are inside the actual object aka in its “private” area.

    • Harendra Singh

      I think both the reasons are valid.
      The fact that iteration on weakMap is not supported helps in keeping the variables actually private and removing old references helps preventing memory leaks.

    • Paul Shan

      Even if I use a Map or even a simple object in my class’s private scope, some other class or even derived class can not access that or its properties. Only issue with this is the memory leak. Thus WeakMap.