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
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