Map and WeakMap
Questions 🤔
- Why Map when there is a Object
- Do Map allow
NaN
as a key? - Map vs Object
- Map vs WeakMap
- What are the application of WeakMap
Map
Map is a key/value data structure introduced in ES6. Map can use any data type(primitive and objects) as a key and can maintain the order of its entries. Maps have elements of both Objects (a unique key/value pair collection), but are more similar to Objects conceptually.
Methods and properties are
- map.set(key, value) – stores the value by the key
- map.get(key) – returns the value by the key, undefined if key doesn’t exist in map
- map.has(key) – returns true if the key exists, false otherwise
- map.delete(key) – removes the value by the key
- map.clear() – removes everything from the map
- map.size – returns the current element count
Salient features of Map
🔸 The iteration goes in the same order as the values were inserted. Map preserves this order, unlike a regular Object, So [...map]
or [...map.keys()]
has a particular order
🔸 Map key comparison is roughly the same as strict equality ===
which uses SameValueZero Algorithm, but the difference is that NaN
is considered equal to NaN
. So NaN
can be used as the key as well.
🔸 Every map.set
call returns map itself, so we can chain
the calls
Iteration over Map
For looping over a map, there are 3 methods:
- map.keys() – returns an iterable for keys
- map.values() – returns an iterable for values
- map.entries() – returns an iterable for entries
[key, value]
, it’s used by default infor..of
Object.entries: Map from Object
When a Map is created, we can pass an array (or another iterable) with key/value pairs for initialization
Map | Object | |
---|---|---|
Key Type | Accepts any type as Key | Only accept string as a key |
Traversal | Maps are traversal can traverse easily with for...of | Traversal is possible by obtaining key |
Default Values | None | Inherited keys from object |
WeakMap
JavaScript engine stores a value in memory while it is reachable (and can potentially be used)
Usually, properties of an object or elements of an array or another data structure are considered reachable and kept in memory while that data structure is in memory.
For instance, if we put an object into an array, then while the array is alive, the object will be alive as well, even if there are no other references to it.
Similar to that, if we use an object as the key in a regular Map, then while the Map exists, that object exists as well. It occupies memory and may not be garbage collected.
WeakMap
is fundamentally different in this aspect. It doesn’t prevent garbage-collection of key objects
WeakMap
do allow only object
as a key. Primitive values for a key is not acceptable
Similarly like map
if we use an object as the key in it, and there are no other references to that object – it will be removed from memory (and from the map) automatically.
WeakMap methods
- weakMap.get(key)
- weakMap.set(key, value)
- weakMap.delete(key)
- weakMap.has(key)
WeakMap does not support iteration methods and property keys(), values(), entries() and size so there’s no way to get all keys/values and total size from it.
Applications of WeakMap
- WeakMap can be used when there is a limited memory
- This can be used for
caching
. Usually in caching service we refresh the contents with new content and delete the older one
Some Facts about WeakMap
WeakMap
in javascript does not hold any keys or values, it just manipulate key value using a unique id and define property to key object. because it define property to key by method Object.defineProperty(), key must not be primitive type.WeaKMap does not contain actually key value pairs, we cannot get length property of weakmap.
Manipulated value is assigned back to key, garbage collector easily can collect key if it in no use
Reference