Simply put, if a variable or object is "unreachable", then there is no need for the variable or object to continue to be stored in memory and should be recycled.
For example:
let xiaoming = {name:'xiaoming'}//Create an object and use the variable xiaoming to reference xiaoming = null //Make the variable xiaoming blank, making the object {name:'xiaoming'} unreachable//{ name:'xiaoming'} Object is recycled
If an object is referenced by an array and other objects, as long as the array and object that reference it exist in the array, then the object is considered reachable.
Objects in the array:
let xiaoming = {name:'xiaoming'} let arr = [xiaoming] xiaoming = null //Make the variable xiaoming blank //The object {name:'xiaoming'} will not be released because it exists in the array.
Similarly, if we use an object as the key of Map
, if Map
exists, then The object will not be recycled by the engine.
Key object in Map
:
let xiaoming = {name:'xiaoming'} let map = new Map() map.set(xiaoming,'a boy') xiaoming = null //Make the variable xiaoming blank //The object {name:'xiaoming'} will not be released because it is the key of the map.
WeapMap
is fundamentally different from Map
in the processing of releasing key objects. To put it simply
, WeapMap
will not prevent garbage collection because of objects as keys.
The difference between WeakMap
and Map
can be divided into three aspects:
WeakMap
can only use objects as keyslet weakMap = new WeakMap() let obj = {name:'obj'} weakMap.set(obj,'obj as the key') weakMap.set('str','str as the key')//The error
code execution results are as follows:
It can be seen that when we use strings as key
, the program cannot execute normally.
In other words, if an object has no other references except WeakMap
reference, then the object will be recycled by the system.
For example:
let weakMap = new WeakMap() let obj = {name:'obj'} weakMap.set(obj,'obj as the key') obj = null //Make the variable obj empty //At this time, the object {name:'obj'} will be recycled
WeakMap
supports limited methodsWeakMap
does not support iterationWeakMap
does not support keys()
WeakMap
does not support values()
WeakMap
does not Supports entires()
, so we have no way to get all key-value pairs.
WeakMap
can only use the following methods:
weakMap.get(key)
Get the key-value pairweakMap.set(key,val)
Set the key-value pairweakMap.delete(key)
Delete the key-value pairweakMap.has(key)
Determine whether itexists The data access method of WeakMap
must be restricted because the timing of the JavaScript
engine releasing the object cannot be determined.
When an object loses all references, JavaScript
engine may release the space occupied by the object immediately, or it may wait a while.
Therefore, at a certain moment, the number of elements of WeakMap
cannot be determined. (Imagine, if we traverse the elements of WeakMap
after an object loses all references, we may get different results.)
The application scenario of WeakMap
is usually to store data that "belongs" to an object. When the object does not When present, the data "belonging" to this object should also be released.
There is a historical story that is very suitable for using WeakMap`: "The cunning rabbit dies, the lackeys are cooked; the birds are gone, and the good bow is hidden."
If we describe this story in JavaScript
code, we should use WeakMap
:
let weakMap = new WeakMap() let rabbit = {name:'rabbit'} //The cunning rabbit let runDog = {name:'runDog'} //The running dog let flyBird = {name:'flyBird'} //The flying bird let goodBow = {name:'goodBow'} //Liang Gong weakMap.set(rabbit,runDog) weakMap.set(flyBird,goodBow) rabbit = null //The cunning rabbit dies flyBird = null //The birds are gone//Soon, the lackeys and the good bow will be released, maybe not immediately //This story tells us that there is no good end to being a lackey, maybe not immediately Killed by //, but it will be killed sooner or later.Compared with Set,
Set
WeakSet
following differences:
WeakSet
can only add object elementsWeakSet
does not prevent the system from recycling elements.WeakSet
supports add()
, has()
, delete()
WeakSet
does not support size
attribute and keys()
method.We can use WeakMap
to verify some existence information, or verify status such as "yes/no". For example, we can use WeakMap
to determine whether the user is online:
let onlineUser = new WeakMap() let zhangSan = {name:'Zhang San'} let liSi = {name:'李思'} let wangEr = {name:'王二'} let maZi = {name:'Mazi'} function login(user){ ... ... onlineUser.add(user) } //Determine whether the user is online function isOnline(user){ return onlineUser.has(user) }
The limitation of WeakMap
and WeakSet
is that they cannot iterate and obtain all elements at once, which does not affect their important role in very critical places.
WeakMap
can only use objects as keys. When all external references to the key are lost (no other variables except WeakMap
refer to the key object), WeakMap
will not prevent the engine from recycling the key value. Once recycled, the corresponding elements of WeakMap
no longer exist.WeakSet
can only store objects. Once the object element loses all external references (except for WeakSet
, no other variables refer to the element object), WeakSet
will not prevent the engine from recycling the element. Once recycled, the corresponding elements in WeakSet
disappear.clear()
, size
, keys()
, values()
are not supported. WeakMap
and WeakSet
are often used to store data structures associated with the "main" object. Once the "main" object loses its meaning, the corresponding associated data structure is naturally deleted.