Map,WeakMapSet,WeakSet

WeakMap and WeakSet are both weak references

1. What is weak reference

A weak reference is a reference that cannot be guaranteed that the object it refers to will not be collected by the garbage collector; in other words, it may be collected at any time.

Weak references disappear at any time, and traversal mechanisms cannot guarantee the existence of members

2,Set

  • Traversal order: Insert order
  • There are no keys and only values, so the key and value can be considered equal
  • Add multipleNaN, there will only be oneNaN
  • When the same object is added, it is considered a different object
  • No type conversion occurs when values are added (5! = = "5")
  • keys()values()In exactly the same way,entries()The traverser returned contains both a key and a value that are equal

3,WeakSet

  • andSetThe structure is similar in that member values can only be objects
  • storageDOMNodes:DOMThis member is automatically released when nodes are removed without worrying about memory leaks when these nodes are removed from the document
  • To temporarily store a group of objects or information bound to them: as long as these objects disappear externally, it will remainWeakSetReferences in the structure are automatically eliminated
  • Members are weak references and are not considered by the garbage collection mechanismWeakSetStructure a reference to this member
  • The member is not suitable for reference, it will disappear at any time, thereforeES6provisionsWeakSetThe structure is not traversal
  • When other objects no longer reference a member, the garbage collection mechanism automatically reclaims the memory occupied by the member, regardless of whether the member still existsWeakSetIn the structure

4,Map

  • Traversal order: Insert order
  • If the same key is assigned more than once, the subsequent value overrides the previous value
  • A reference to the same object is treated as a key
  • Two instances of the same value are treated as two keys
  • The key is bound to the memory address, and as long as the memory address is different, it is regarded as two keys
  • Add more than oneNaNAs a key, there will only be oneNaNAs the value of the key
  • ObjectStruct provides string-value correspondence,MapStructure provides value – value correspondence

5,WeakMap

  • andMapSimilarly structured, member keys can only be objects
  • storageDOMNodes:DOMThis member key is automatically released when nodes are removed without worrying about memory leaks when they are removed from the document
  • Deploy private properties: Internal properties are weak references to the instance and disappear when the instance is deleted without causing a memory leak
  • Member keys are weak references and are not considered by the garbage collection mechanismWeakMapStructure a reference to this member key
  • The member key is not suitable for reference, it will disappear at any time, soES6provisionsWeakMapThe structure is not traversal
  • When other objects no longer reference a member key, the garbage collection mechanism automatically reclaims the memory occupied by the member, regardless of whether the member still existsWeakMapIn the structure
  • Once no longer needed, the member disappears automatically, without manually removing the reference
  • Weak references are only keys, not values, and values are still normal references
  • Even if references to member keys are removed externally, the internal member values remain

6,ObjecttoMap

let obj = {"a":1."b":2};
let map = new Map(Object.entries(obj))
Copy the code