This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

background

Learning the front end of three months, ready to brush interview questions, summary summary, a few interview questions a day, to the big factory.

The problem

Introduce the Set, WeakSet, Map, WeakMap, WeakRef, FinalizationRegistry!

Set

  1. The value of the member is unique and does not duplicate. (Either object or reference)
  2. It’s like an array, just values.
  3. Set have increased (add), delete (delete), iteration (keys, values, entries, forEach), check (has), and other methods.

According to the nature of Set, often used for array deduplication!

Such as:

var a=[2.3.4.5.6.3.4.7];

var f=[...new Set(a)];

console.log(f);
// [2, 3, 4, 5, 6, 7]
Copy the code

It can also be used to remove the same letter from a string

var a=[2.3.4.5.6.3.4.7];

var f=[...new Set(a)];

console.log(f);
// [2, 3, 4, 5, 6, 7]
Copy the code
var str1="abcdefadc";


var e=[...new Set(str1)];

console.log(e);

//[ 'a', 'b', 'c', 'd', 'e', 'f' ]
Copy the code

WeakSet

When we understand Set, we can understand WeakSet well. WeakSet also has the non-repeatable characteristics of Set, but the difference between it and Set is:

WeakSet members can only be objects, not other types of values, including symbol values.

Another special note is that the objects in the WeakSet are weak references and will not be counted by the garbage collection mechanism, that is to say, the reference of the member objects in the WeakSet will not increase the count in garbage collection.

The specific method is similar to Set.

Map

  1. Is essentially a set of key-value pairs (has structure)
  2. Keys are not limited to strings; they can be objects.
  3. Keys are unique (bound to memory addresses) and are compared in strict equality.
  4. It also has the method of increase, delete, change and check.

The transformation of maps to other data structures is used in some special situations. Just understand.

WeaKMap

Differences with Map:

  1. Only objects are accepted as key names (null).
  2. WebMap keys point to objects that are not counted in the garbage collection mechanism. (Weak references only contain key names, not key values)
  3. There are no traversal methods, only get,set, HAS and delete methods.

Here we see what this object is mainly used for. It is also used to establish weak references. The function is similar to WeakSet. Garbage collection of objects is not affected, helping to prevent memory leaks.

WeaKRef

WeakSet and WeakMap are data structures based on weak references, while WeakRef is a weak reference that directly creates an object.

Also used to prevent memory overflow.

var target = {a:1};
var wr = new WeakRef(target);
Copy the code

WeakMap instances provide a deref () method that can be used to determine whether an object has been garbage collected and return the original object if it still exists, otherwise undefined.

FinalizationRegistry

FinalizationRegistry is a system-provided constructor that calls the callback function to tell you when your registered object is garbage collected.

// Register a callback
const registry = new FinalizationRegistry(heldValue= > {
  / /...
});

// register theObject to listen on. When theObject is reclaimed, "some value" is passed in as a callback.
// If you also want to cancel listening, you pass in a third parameter as a marker, usually the original object. Call the unregister method to cancel the object before it is reclaimed.
registry.register(theObject, "some value",theObject);

Copy the code

conclusion

One step at a time, solid work!