Check out a free preview of the full Advanced Web Development Quiz course

The "Q13: WeakMap" Lesson is part of the full, Advanced Web Development Quiz course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to analyze the code and determine which statement or statements about the WeakMap is true.

Preview
Close

Transcript from the "Q13: WeakMap" Lesson

[00:00:00]
>> Alright what statements are true, userTokenMap implements the iterator protocol, when setting user to null userTokenMap returns zero. See if the user object is set to null, its userTokenMap entry can be garbage collected or D, spreading the userTokenMap in an array returns an array of the userTokenMap's entries. All right, so the only correct answer is C, if the user object is set to null, its userTokenMap entry can be garbage collected.

[00:00:36]
So let's take a look at what and weak map, is which we've defined here. So first we have the userTokenMap, which is a weak map it gets stored in the heap. And then we have the user Jane Doe age 24, also stored in heap referenced from the global execution context.

[00:00:50]
Then we set the userTokenMap, we create an entry with the user object as its key and like a secret token as its value. Now, the nice thing about weak maps is that they actually reference that object, that we defined as the entry through something called a weak reference.

[00:01:06]
And, this only kind of makes sense when we eventually delete the user, cuz now also the entry will be removed. So, when we compare this to a regular map, so not a weak map. So now it's the exact same, but now you can see that it has a strong reference to the user through that entry.

[00:01:23]
So now if we were to delete that user, that user object would still exist because as I talked about prior objects do not get deleted or garbage collected. When there is still a reference from the global to that object and in here, we still have a reference there through the userTokenMap.

[00:01:39]
So even though it's not directly accessible, they're still like an indirect reference, through the userTokenMap. So, this can be very useful in the case of the weak map here, cuz maybe we just want to add some attributes to an object, that shouldn't necessarily be part of the object like in here, like a secret token.

[00:01:58]
And we wanna make sure that maybe when the user logs out, which somehow removes some kind of object that we passed as a value to that map. That entry also gets removed, cuz otherwise we just have all these objects in memory and this could create like the object will not be able to get garbage collected resulting in a memory leak.

[00:02:18]
And you sometimes see this on like DOM nodes, like that's what a lot of people use week maps for. Because we can add some attributes to a DOM node, using a weak map. And then when the DOM node eventually gets removed from the DOM, the entire entry doesn't exist anymore.

[00:02:34]
But we can still access the the values through the weak map, by getting like userTokenMap.get and then the user or in that case the DOM attribute. Yeah, if it doesn't exist any more, if it gets removed or in the case of the DOM it unmounts then that's also just gone.

[00:02:51]
There's no reference any more in memory, so we're not causing that accidental memory leak, if that makes sense. You also have like weak set, which works in the same way it just uses like a weak reference to the objects to ensure that. If it's not used anymore or if it's removed, then just remove the object instead of keeping that reference like here, in which case it doesn't get removed, make sense?

[00:03:18]
It's like pretty niche use cases for weak maps, like, but yeah, it's very useful once you have a use case for it, you're this makes total sense. In the case of maybe setting a token, maybe authentication token, which probably you shouldn't store in a week map is pretty insecure.

[00:03:34]
But anything that should be removed once an object has been removed as well. So you can only pass objects and I believe functions as an entry to maps for that reason. Cuz it needs to be referenced and strings numbers, any primitive data type is not a reference, that's just copy memory.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now