Strong reference

In JS, if we save a reference through a variable or constant, that variable or constant is a strong reference. Internally, a line connects the variable to the reference address so that the reference is not garbage collected. If we assign a new variable to the reference, a new “line” will be created in memory to connect the new variable to the reference.

const user = {name:"alex"}
const user2 = user
Copy the code

In the code above, we assign a reference: {name:”alex”} to user. In real memory, there would be a wire connecting the user to the reference:

We then create a variable, user2, and assign user to it. The value assigned here is actually the reference, so it looks like this:

Next, let’s take a look at what happens when we use the new types WeakSet and WeakMap introduced in ES6 to store reference values.

A weak reference

There are many new types introduced in ES6, including WeakSet and WeakMap. Both types can only receive reference value storage, and these reference values are weak references. Next, we focus on introducing such weak reference characteristics from WeakSet, so we look at the following code:

// The same definition as before
const user = {name:"alex"}
const user2 = user
​
// We use WeakSet to create a WeakSet instance
const ws = new WeakSet(a)// We add a data to the instance ws constructed by WeakSet: user2
ws.add(user2)
Copy the code

So far, we have done two more things. The first is that we created a WeakSet instance, and the second is that we added a user2 to the instance whose reference value is {name:” Alex “}. Then we use the graph to represent the following situation:

As you can see, the value {name:” Alex “} in the WS instance refers to {name:” Alex “} (in real memory it refers to a pointer reference to the stack of that data, which points to the corresponding value in the heap at that address). And it is important to note that the weak reference “line” is transparent. What does this mean? What’s the difference between him and a strong quote? Summary: A strong reference is recognized as a “connection” by {name:” Alex “}, while a weak reference is not recognized. That is, the reference does not know that it is referenced by the WS instance. The consequence is that the reference does not know that it is referenced by the WS instance, which means that the garbage collection does not know that the reference is referenced by the WS instance either. So if all strong reference connections for the reference are broken (the variable is assigned to null or otherwise), the reference is destroyed as garbage, even if the WS instance is still referencing the reference.

// The same definition as before
const user = {name:"alex"}
const user2 = user
​
// We use WeakSet to create a WeakSet instance
const ws = new WeakSet(a)// We add a data to the instance ws constructed by WeakSet: user2
ws.add(user2)
user = null
user2 = null
console.log(ws) / / {}
Copy the code

Continuing with our code above, we add a new value to the WS instance that points to the reference {name:” Alex “}, at which point we assign null to both user and user2, disconnecting their strong reference connection. So what happens?

At this point, because all strong references are broken, the garbage collector considers the reference {name:” Alex “} no longer needed and destroys it. Then the reference used by the corresponding WS instance no longer exists, even if the WS instance is still using the reference. This is the nature of weak references.

Sprout new just learn, expression this may not be too clear, forgive me.