This is the 15th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

ES6 added Set, WeakSet, Map, WeakMap data structure, today let’s have a look

A, the Set

A Set is a data structure similar to an array, except that all members of a Set are unique. You can think of a Set as an array that has neither repeating elements nor any notion of order. Set itself is a constructor that is used to generate the Set data structure

const s1 = new Set()
s1.add(1)
s1.add(2)
s1.add(1)
// s1 = {1, 2}
Copy the code

The Set () function takes as an argument a looping data structure such as a Set, a class array, or any other data structure that contains an iterable interface

const s2 = new Set([1.2.1.4.3.4])
// s2 = {1, 2, 3, 4}
Copy the code
  1. Common properties and methods

(1) Size is used to view the number of members of a Set subclass object

const s2 = new Set(['a'.'b'.'c'.'d']);
// s2 = {"a", "b", "c", "d"}
s2.size / / 4
Copy the code

(2) Add (value) is used to add a value to a set, returning the set structure itself, so you can use the chained call (3) Delete (value) is used to delete a value, (4) has(value) is used to find out whether the Set contains a value and returns a Boolean value. (5) Clear () is used to clear all members. No value is returned

(1) keys() : return key names (2) values() : return key values(3) Entries () : return all key-value pairs The member Set structure has no key names, only keys(or keys are the same), so the key and value methods behave exactly the same. The entries method returns both a key name and a key value, so it prints an array one at a time whose members are exactly equal.

Second, the WeakSet

The structure of a WeakSet is similar to a Set in that it is a collection of non-repeating values. However, it differs from Set in two ways

  1. WeakSet Members of can only be objects and cannot be values of any other type.
  2. WeakSetAre weak references, that is, the garbage collection mechanism does not considerWeakSetA reference to the object, that is, if no other object references the object, then the garbage collection mechanism automatically reclaims the memory occupied by the object, regardless of the existence of the objectWeakSetAmong the
Third, the Map

Like an object, it is a collection of key-value pairs, but the range of “keys” is not limited to strings, and any type of value can be used as a key

  1. Map attribute and operation method

(1) size (2) set(key, value), get(key) set(key, value), get(key) set(key, value), get(key) Return undefined if none; Get (key) Is used to return the key named key. (3) Has (key), delete(key), clear() and Set use has, delete, and clear in similar ways

2. The Map cycle

You can use keys, values, entries, or forEach to loop through a Map instance. The difference between a Map instance and a Set is that the Map instance has different key names and values

Four, WeakMap

Similar to the Map structure, the WeakMap structure is also used to generate a collection of key and value pairs. The difference is that WeakMap does not prevent its key values from being garbage collected, which means you can associate data with objects without worrying about memory leaks

Summary:

  1. Obvious in the course of useSetandMapThan we used to use a lotArrayandObjectThere are obvious convenient advantages, so in the future development, we can according to the scene needs to passSetandMapTo replace theArrayandObjectTo use the
  2. If there is a need for unique data structure storage, consider usingSet
  3. If the data is complex, consider using itMapIs very popular in some build toolsMapThis data structure for configuration becausemapIs a flexible, efficient, suitable for one – on – one search data structure
  4. SetandMapHave a similarAPI, the main difference isSetThere is nosetMethod, since it can’t store key-value pairs, the rest is almost identical
  5. WeakMapWeakSet As a relatively new concept, its main characteristic is weak reference
  6. Compared to theMap withSet Weak references allow objects to be correctly garbage collected in the “appropriate” circumstances, reducing memory waste