Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

es6-articles / 66 - Added missing 'weak' reference #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 66 - WeakMap and Garbage Collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const weak = new WeakMap();


strong.set(dog1, 'Snickers is the best!');
strong.set(dog2, 'Sunny is the 2nd best!');
weak.set(dog2, 'Sunny is the 2nd best!');
```

Notice how we're using the keys here, which are objects.
Expand All @@ -37,7 +37,7 @@ const weak = new WeakMap();


strong.set(dog1, 'Snickers is the best!');
strong.set(dog2, 'Sunny is the 2nd best!');
weak.set(dog2, 'Sunny is the 2nd best!');

dog1 = null;
dog2 = null;
Expand All @@ -49,4 +49,4 @@ We have our `strong`, which still has an item in it. However `weak` has nothing

We no longer have a `dog1`, and we no longer have a `dog2`, but the `strong` one is still holding onto this object that no longer exists. That's a memory leak there, because there's no way for us to reference it, whereas the WeakMap has totally cleaned it up. Garbage collection has happened and we no longer reference it there.

If you need something like that, where it gets garbage collected, where you don't have to babysit what is and isn't inside of the map, then that's the case where you reach for a WeakMap over a Map.
If you need something like that, where it gets garbage collected, where you don't have to babysit what is and isn't inside of the map, then that's the case where you reach for a WeakMap over a Map.