Link to github.com

ES6 provides new data institutions: Set, WeakSep, Map, and WeakMap.

Set

Data structure Set type array, but no duplicate values, values are unique, values can be any type. The Set itself is a constructor, and the corresponding structure can be created directly with new.

const set = new Set(a);// or passes the initialization array
const set1 = new Set([1.2.3])
// Or passes in other data structures with iterable interfaces
const set2 = new Set(document.querySelectorAll('div'));
Copy the code

Because the elements in a Set data structure are not duplicated, it can be used as a way to de-duplicate arrays

[...new Set(array)]
Copy the code

Properties and methods

attribute

  • Set. The prototype. The constructor: constructor, the default is Set function.
  • Set.prototype.size: Returns the total number of members of a Set instance.

methods

  1. Operation function
  • Set.prototype.add(value) : Adds a value and returns the Set structure itself.
  • Set.prototype.delete(value) : Deletes a value and returns a Boolean value indicating whether the deletion was successful.
  • Set.prototype.has(value) : Returns a Boolean value indicating whether the value is a member of Set.
  • Set.prototype.clear() : Clears all members with no return value
const set = new Set(a);// Since the add method returns the Set structure itself, it can operate continuously
set.add(1).add(3);
Copy the code
  1. Traversal functions
- Set.prototype.keys() : returns key name traverser -Set.prototype.values() : iterator that returns key values -Set.prototype.entries() : Returns key value pair traversal -Set.prototype.foreach () : Use the callback function to iterate over each memberCopy the code

WeakSet

WeakSet data structure and Set type, there is no duplicate value, value is also unique, but members can only be objects, and not other types of value.

For the detail introduction of WeakSet, Teacher Ruan Yifeng has explained it in great detail. There is no supplement here, but it is directly quoted. If necessary, you can check the original text

Secondly, the objects in WeakSet are weak references, that is, garbage collection mechanism does not consider WeakSet’s reference to the object, that is to say, if other objects no longer reference the object, then garbage collection mechanism will automatically recover the memory occupied by the object, not considering the object still exists in WeakSet.

This is because the garbage collection mechanism relies on reference counting and does not free the memory if the number of references to a value is not zero. After you finish using the value, you sometimes forget to dereference it, causing memory to be unable to be freed, which can lead to a memory leak. WeakSet references are not included in the garbage collection mechanism, so there is no problem. Therefore, WeakSet is suitable for temporarily storing a group of objects and information bound to the object. As soon as the object disappears externally, its reference in WeakSet disappears automatically.

Due to the above feature, a WeakSet member is not suitable for reference because it will disappear at any time. In addition, because the number of internal WeakSet members depends on whether garbage collection mechanism is running, the number of members may be different before and after operation, and when garbage collection mechanism is running is unpredictable, so ES6 stipulates that WeakSet cannot be traversed.

Properties and methods

attribute

  • WeakSet. Prototype. Constructor: constructor, the default is WeakSet function.

methods

  • Weakset.prototype. add(value) : Adds a new member to the WeakSet instance.
  • Weakset.prototype. delete(value) : Clears the specified member of a WeakSet instance.
  • Weakset.prototype. has(value) : Returns a Boolean value indicating whether a value is in a WeakSet instance.

Map

Data structures Map and Object are essentially collections of key-value pairs (Hash structures). The Map data structure is mainly used to solve the limitation that the key of the original Object can only be a string. Map’s “keys” support various types of structures;

Properties and methods

attribute

  • The Map. The prototype. The constructor: constructor, the default is the Map function.
  • Map.prototype.size: Returns the total number of Map instance members.

methods

  1. Operation method
  • Map.prototype.set(key, value) : set the key value, the value added later will override the value added earlier.
  • Map.prototype.get(key) : retrieves the corresponding key, return undefined if none exists.
  • Map.prototype.has(key) : Checks whether a key is in the current Map object.
  • The Map. The prototype. The delete (key); Returns a Boolean value indicating whether the deletion was successful.
  • The Map. The prototype. The clear (); Clears all members, no return value.
  1. Traversal methods
  • Map.prototype.keys() : iterator that returns key names.
  • Map.prototype.values() : iterator that returns key values.
  • Map.prototype.entries() : Returns a traverser for all members.
  • Map.prototype.foreach () : Iterates through all members of the Map.

WeakMap

WeakMap structure is similar to Map structure and is also used to generate a set of key-value pairs. WeakMap only accepts objects as key names (except null) and does not accept other types of values as key names; The object pointed to by the key name of WeakMap is not included in the garbage collection mechanism;

Properties and methods

attribute

  • WeakMap. Prototype. Constructor: constructor, the default is WeakMap function.

methods

  1. Operation method
  • Map.prototype.set(key, value) : set the key value, the value added later will override the value added earlier.
  • Map.prototype.get(key) : retrieves the corresponding key, return undefined if none exists.
  • Map.prototype.has(key) : Determines whether a key is in the current WeakMap object.
  • The Map. The prototype. The delete (key); Returns a Boolean value indicating whether the deletion was successful.
  1. Traversal methods

There is no

conclusion

Summarize the differences between Set, Map, WeakSet and WeakMap

  • Set – an object that allows you to store a unique value of any type, either a primitive value or an object reference;
  • WeakSet – Members are objects; Members are weak references, which can be collected by the garbage collection mechanism. They can be used to store DOM nodes and are not prone to memory leaks.
  • Map – essentially a collection of key-value pairs, similar to a collection; Can traverse, can convert to a variety of data formats;
  • WeakMap – Only accepts objects as key names (except null), does not accept other types of values as key names; The key name is a weak reference, the key value can be arbitrary, the object that the key name points to can be garbage collected, the key name is invalid; Cannot traverse;

The resources

Set and Map data structures