1
00:00:02,230 --> 00:00:06,670
Now with weak map, we have a similar idea as with weak set.

2
00:00:06,670 --> 00:00:07,960
So I have my person here,

3
00:00:07,990 --> 00:00:14,350
let's say I didn't reset it yet and instead now, I'll also want to have my additional person data here.

4
00:00:14,590 --> 00:00:22,370
So person data again can be a map but then if we use that person here as a key, we have the same as

5
00:00:22,370 --> 00:00:24,470
with the set, with the normal set.

6
00:00:24,470 --> 00:00:32,330
If we clear person, if the map is a normal map and we have person as a key there, the map will hold onto

7
00:00:32,330 --> 00:00:37,960
it and not release it for garbage collection, might be what you want but if you know you don't need that,

8
00:00:38,150 --> 00:00:40,200
you could use a weak map instead.

9
00:00:40,310 --> 00:00:45,940
Now on a weak map, you can of course also call a couple of methods, way less than before

10
00:00:45,950 --> 00:00:50,450
because again you can't loop through the entries because the entries, the amount of entries and so on

11
00:00:50,450 --> 00:00:56,090
is simply not guaranteed which is why the size property is also not available. Javascript can't really

12
00:00:56,090 --> 00:01:00,370
tell what's in the map because it can look at it

13
00:01:00,470 --> 00:01:05,870
if you ask it with has and get if something is in there but other than that, it doesn't know if something

14
00:01:05,870 --> 00:01:12,030
has been released yet, which is why you have that stripped down choice of methods but what you do

15
00:01:12,110 --> 00:01:13,460
have of course is set

16
00:01:13,460 --> 00:01:15,400
and then we can set person as a key

17
00:01:15,980 --> 00:01:22,740
and now here, keys have to be objects so that this garbage collection thing makes sense and any value,

18
00:01:22,770 --> 00:01:29,720
the value does not have to be an object, that could just be a string or whatever you want here.

19
00:01:29,750 --> 00:01:37,100
Now with that same logic as before, as soon as we let go of that person object in all other places, like

20
00:01:37,100 --> 00:01:42,080
here where I set my variable to null and therefore I get rid of the address which was stored in there,

21
00:01:42,080 --> 00:01:47,900
Javascript is able to garbage collect this and the weak map won't stop it from garbage collecting this

22
00:01:47,900 --> 00:01:48,230
here,

23
00:01:48,230 --> 00:01:54,710
so from garbage collecting this person and therefore here, still we will probably see the person if we

24
00:01:54,710 --> 00:02:01,490
log this weak map but eventually that will be garbage collected and it will not be part of that weak

25
00:02:01,490 --> 00:02:03,320
map anymore.

26
00:02:03,320 --> 00:02:04,770
So weak map and weak set,

27
00:02:04,820 --> 00:02:11,360
advanced, very advanced concepts which you will rarely use but which can help you manage memory more

28
00:02:11,360 --> 00:02:18,980
efficient in large applications where you have data which is fine to be deleted at some point which

29
00:02:18,980 --> 00:02:23,930
you might want to manage in a map or a set and where you don't want to take care about manually clearing

30
00:02:23,930 --> 00:02:27,230
data, well then weak map and weak that are great.

31
00:02:27,290 --> 00:02:30,860
As I mentioned, not really something we'll immediately use here,

32
00:02:30,860 --> 00:02:35,870
not really something you'll need in a lot of applications to be very honest but something you should

33
00:02:35,870 --> 00:02:41,390
have at least have heard of if you want to become a Javascript developer so that that is something you

34
00:02:41,390 --> 00:02:47,630
can utilize in bigger applications, more advanced applications and in your future career as a Javascript

35
00:02:47,630 --> 00:02:48,890
developer in general.
