Describing the differences between Map, WeakMap of ES6 and plain Objects

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 🙂