1
00:00:02,320 --> 00:00:08,710
I'm not entirely done with sets and maps yet because there are specialized versions of them.

2
00:00:08,710 --> 00:00:14,720
Let me comment this out and let me introduce you to weak set and weak map.

3
00:00:14,730 --> 00:00:16,270
Now what is that?

4
00:00:16,360 --> 00:00:18,000
Let's say we have a person here,

5
00:00:18,040 --> 00:00:20,620
it's not a constant but a variable, name

6
00:00:20,620 --> 00:00:25,270
Max and I want to store it is an asset.

7
00:00:25,320 --> 00:00:29,680
Now previously we had a set of strings but of course, you can store any data in there,

8
00:00:29,700 --> 00:00:31,830
you can store objects in there as well.

9
00:00:31,830 --> 00:00:34,260
Now in the case of the weak set which I'll show you,

10
00:00:34,260 --> 00:00:37,470
it has to be object data to be precise.

11
00:00:37,470 --> 00:00:46,430
So here persons now is my new weak set which now requires objects to be stored in it,

12
00:00:46,440 --> 00:00:51,180
that means you can also store arrays in there since they are objects in the end but you can't store numbers

13
00:00:51,180 --> 00:00:52,700
or strings in there.

14
00:00:52,710 --> 00:00:58,710
So here I'll actually initialize this to be an empty set and now I can add a new entry with the add

15
00:00:58,710 --> 00:01:00,200
method and add person.

16
00:01:00,360 --> 00:01:05,910
Now right away, when you type persons., you see there are way less methods available, only add,

17
00:01:05,940 --> 00:01:11,370
delete and has. There is no method to get all the entries for example

18
00:01:11,370 --> 00:01:18,420
and the reason for that is that weak set, just like weak map which I will also show you internally works

19
00:01:18,480 --> 00:01:25,530
such that it can only store objects so that it can actually clear these objects for you, release them

20
00:01:25,530 --> 00:01:29,620
to garbage collection which I covered in the behind the scenes module,

21
00:01:29,790 --> 00:01:35,210
if you don't work with a certain piece of data anymore. Conveniently if we console log persons here,

22
00:01:37,090 --> 00:01:43,780
the browser dev tools console still allows me to look inside of that, if we reload here, we can actually

23
00:01:43,780 --> 00:01:46,570
have a look into our set and see the value here.

24
00:01:46,570 --> 00:01:50,600
Now you might wonder, why would I need this strange thing?

25
00:01:50,620 --> 00:01:53,310
Well if you have an application where you store data,

26
00:01:53,350 --> 00:02:02,440
let's say in a set, object data in a set or other arrays, where you eventually will let go of data, then

27
00:02:02,440 --> 00:02:06,690
you want to make sure that this data can be garbage collected.

28
00:02:06,700 --> 00:02:13,930
Now if we work with that person here let's say and then we do some operations with it and we

29
00:02:13,930 --> 00:02:19,330
needed it in the set for these operations and we still need the set thereafter, maybe for other operations

30
00:02:19,450 --> 00:02:21,950
but this person is not really required anymore,

31
00:02:22,060 --> 00:02:24,370
then we can set person equal to null.

32
00:02:24,370 --> 00:02:26,860
This means we set this variable to null,

33
00:02:26,890 --> 00:02:31,410
so this object no longer has anyone who is interested in it.

34
00:02:31,510 --> 00:02:35,410
This person variable, which stored its address is now reset to null,

35
00:02:35,500 --> 00:02:42,340
so the address is released from there and the Javascript engine then is able to pick this up and eventually

36
00:02:42,460 --> 00:02:44,400
clear this object from the heap,

37
00:02:44,410 --> 00:02:48,720
that's what garbage collection does, it clears that data from memory.

38
00:02:48,730 --> 00:02:55,510
Now if you would use a normal set here which you can normally, then the person you created here would

39
00:02:55,510 --> 00:02:57,020
still be part of that set.

40
00:02:57,040 --> 00:03:03,350
So even if you cleared your variable here, you added that object, that pointer at this object to this

41
00:03:03,340 --> 00:03:11,350
set and Javascript thankfully detects that and will not clear the object because the set still holds

42
00:03:11,350 --> 00:03:17,260
a reference to it and that's good because Javascript would expect that you plan on working with that

43
00:03:17,260 --> 00:03:19,470
and deleting it therefore would be bad.

44
00:03:19,480 --> 00:03:25,360
Now let's say you eventually never end up working with that person again, then with a normal set, you have

45
00:03:25,360 --> 00:03:31,840
that unnecessary data stored in memory because you have that data in a set even though you don't

46
00:03:31,840 --> 00:03:33,000
need it anymore.

47
00:03:33,010 --> 00:03:37,240
Sure, you could have done persons delete and deleted that value

48
00:03:37,270 --> 00:03:41,800
but if you forgot that, well there you go. Now with weak set,

49
00:03:41,800 --> 00:03:47,740
if you reset all other places where you point that this object, the weak set will not hold onto it.

50
00:03:47,920 --> 00:03:53,500
So the weak set allows garbage collection to delete items that are part of the set

51
00:03:53,500 --> 00:03:57,110
as long as no other part of your code uses these items,

52
00:03:57,160 --> 00:04:03,470
so now with that eventually, that will be garbage collected. Here if I reload, we can still see that person,

53
00:04:03,580 --> 00:04:08,830
so it certainly didn't get garbage collected between this line and this line but garbage collection

54
00:04:08,830 --> 00:04:09,790
can't be planned,

55
00:04:09,790 --> 00:04:11,740
you don't know when the browser does it.

56
00:04:11,740 --> 00:04:18,070
The thing is with this here and weak set being used, it's able to garbage collect this. So weak set is an

57
00:04:18,070 --> 00:04:24,850
interesting option in cases where you store object data in a set and you eventually release some of

58
00:04:24,850 --> 00:04:28,690
that data and you want to make sure that this thing can be garbage collected.

59
00:04:29,110 --> 00:04:34,660
Obviously super advanced, not something you'll use in many applications, not something we need here in

60
00:04:34,660 --> 00:04:35,830
the majority of this course

61
00:04:35,830 --> 00:04:41,980
to be honest but something to be aware of, especially as you grow as a developer and you will encounter

62
00:04:41,980 --> 00:04:44,140
more and more advanced programs,

63
00:04:44,200 --> 00:04:48,400
advanced code snippets where something like that might get used.
