Weak References, Reachability, WeakSets, and WeakMaps

  Рет қаралды 3,416

Steve Griffith - Prof3ssorSt3v3

Steve Griffith - Prof3ssorSt3v3

Күн бұрын

This tutorial explains the concepts of Strong Reference versus Weak Reference plus Reachability in JavaScript. This is followed by a discussion and examples of WeakSet and WeakMap.
code from video: gist.github.com/prof3ssorSt3v...
0:00 Strong Ref & Reachability
3:50 WeakSets
9:20 WeakMaps

Пікірлер: 31
@leonardchan2638
@leonardchan2638 8 ай бұрын
By far the best and clearest explanation for WeakSet and WeakMap 👍🏻 Although I can't imagine how and when I will use them effectively.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 8 ай бұрын
They're not an everyday thing
@a1r592
@a1r592 Жыл бұрын
Thanks for the video, very clear explanation!
@ofeenee
@ofeenee Жыл бұрын
Thank you for the great video! 🙏
@kosnowman
@kosnowman 10 ай бұрын
this is amazing thank you Steve!
@pasej5
@pasej5 5 ай бұрын
You made this so easy to understand thank you.
@shahinza
@shahinza Жыл бұрын
Thank you 🙏
@kerodfresenbetgebremedhin1881
@kerodfresenbetgebremedhin1881 Жыл бұрын
stupendous
@mohitpal1505
@mohitpal1505 8 ай бұрын
let person = { id: 123, name: "Mohit" }; const wk = new WeakSet(); wk.add(person); person = null; console.log(wk.has(person)); console.log(wk); When I logged these two lines, the first returned false, as expected. But when I logged the wk WeakSet, it still had a value property which held the underlying object used in my code by person, i.e. { id: 123, name: "Mohit" }. Why did this happen?
@MarkKozel
@MarkKozel Жыл бұрын
Does using weak references improve performance and/or memory usage? Just wondering when I'd want to use it over the strong refs, since they seem to be default
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
Weak references will get garbage collected more aggressively and so will free up memory faster. They give you the ability to store references in a single location that you can use to check for existence. Then they automatically get removed as other references in your code get removed.
@MikkelGissel
@MikkelGissel Жыл бұрын
Wuhuuu, riding the edge of jS, U feel i peeps?! Maybe some neck beard C coders will finally see what the fuzz is all about xD Thx Steve!
@Arcanist1010
@Arcanist1010 Жыл бұрын
May i know what font name you use?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
Jet Brains Mono
@unauthorizedaccess8062
@unauthorizedaccess8062 Жыл бұрын
Thank you sir for this tutorial! But you haven't explained where and why one should/could use them if there is no looping available? In what cases would one want to put these references in a container? I hope my question is understandable. Thanks again.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
I did explain why. I didn't give practical code samples of Weak references being applied. This is not something that a junior developer would ever use or need to use. They are more a thing that when you need them you will understand why. WeakSets and WeakMaps are more of an advanced tool for improved garbage collection. When you need to have a centralized storage location for items that allows you to check for existence of items. You can have references elsewhere in your code that you are using in your interface. The Weak container could use the interface elements as the keys. You can check with the Weak container to see if the elements still exist in the interface or are referenced from the interface. As strong references are cleared away the objects in the Weak container would also be removed by garbage collection. For less experienced developers the important key here is understanding Reachability and the difference between Weak and Strong references. There is no situation where you MUST use the Weak Collections.
@unauthorizedaccess8062
@unauthorizedaccess8062 Жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Okay. Thank you for explaining.
@pradeepbisht3397
@pradeepbisht3397 Жыл бұрын
i don't understand what are you trying to say let s=new Set() let a={b:"something"}; s.add(a); a=null; console.log("a is present "+s.has(a)); a={b:"something"}; s=new WeakSet() s.add(a); a=null; console.log("a is present "+s.has(a)); both are giving same answer
@agent-33
@agent-33 Жыл бұрын
WeakSets are collections of objects only. They cannot contain arbitrary values of any type, as Sets can.
@agent-33
@agent-33 Жыл бұрын
They both held objects _weakly_ so they are the same in that behavior.
@pradeepbisht3397
@pradeepbisht3397 Жыл бұрын
@@agent-33 doesn't he said something else
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
In your Set example we set a = null. At that point the Set still contains a reference {b: "something"}. If you console the Set you will see it there. The variable a now holds null. The Set does not contain null. With the WeakSet once a is set to null, The WeakSet is no longer allowed to hold the {b:"something"}. It will be empty
@pradeepbisht3397
@pradeepbisht3397 Жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 let s=new Set() let a={b:"something"}; s.add(a); a=null; console.log(s); a={b:"something"}; s=new WeakSet() s.add(a); a=null; console.log(s); I just did it and both answer have {b:"something"} why?
@nikolapuzovic6929
@nikolapuzovic6929 4 ай бұрын
13:00 actually that's not true, any data type can be key in map
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 ай бұрын
Yes. I was thinking of the objects when I said that. Object keys can only be Strings or Symbols.
@makl-the-oracle
@makl-the-oracle Жыл бұрын
Banger
@captenK
@captenK Жыл бұрын
Only watched the Set section, but the code doesn't seem convicing 🤔After you set `son=null` then the check becomes `kids.has(null) // returns false;` That is kind of expected, wouldn't it make more sense to check for `kids.size` ? Show that by removing the ref we lost an element in the set without calling delete directly.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
You don't have the concept yet. WeakSet has no size method. So, you cannot do that. son is a strong reference to the value { name: 'Alex' } in memory. We then add son as an item inside the WeakSet kids. When the variable son is changed from { name: 'Alex' } to null, that means that son no longer has any connection to that { name: 'Alex' } value. Because son was the only strong reference to { name: 'Alex' } the value { name: 'Alex' } can be garbage collected because there are no more strong references to it. The WeakSet will lose { name: 'Alex' } from it's list automatically because the value is gone from memory. The WeakSet doesn't care about "son". It was watching { name: 'Alex' } in memory. When that disappears from memory, the weakSet loses that entry. WeakSets can't loop through the values. They can't check their size. This is because the values it holds can disappear at any point. The WeakSet does not prevent the garbage collection of any value.
@takivilmos
@takivilmos 3 ай бұрын
​@@SteveGriffith-Prof3ssorSt3v3 I think the first part of the argument is still valid. When you say `son = null`, you set son explicitly to null, so you no longer try to access the original object the son variable referenced, but the `null`. If you write `kids.has(son)` after you set `son = null`, it will just be evaluated as `kids.has(null)` which will be false anyway. I'm not sure, please correct me if I'm wrong, but I assume as long as you can reference any value, it is still in memory, so you cannot showcase how it will disappear from the weakmap. If you could keep the reference to call `.has()` with it, it wouldn't be garbage collected so weakmap would still have the value. But when the value gets garbage collected, it means it no longer has any references, so you don't have anything to call `.has()` with. The importance of a weakmap is that if you have no other references, only the weakmap, it won't stop the value from being garbage collected, so it won't cause a memory leak.
@blueml08
@blueml08 2 ай бұрын
I guess you are right. I wrote a simple example with console log and a timeout function. The weakMap still holds a reference, even when on of the objects is already gone. let user1 = {name: 'Sheridan'}; let user2 = {name: 'Mollari'}; let user3 = {name: 'Ivanova'}; const weakMapTest = new WeakMap(); weakMapTest.set(user1, 'green'); weakMapTest.set(user2, 'green'); weakMapTest.set(user3, 'green and purple'); console.log({weakMapTest}); user1 = null; setTimeout(() => { console.log({weakMapTest}); }, 10_000); In Firefox there is an tool for manually triggering the GC. Just open FF and enter about:memory Here you can see the loose reference. Without triggering the GC the values stay inside the weak map. After hitting GC, the {name: 'Sheridan'} object is gone.
@rogierwiertz8522
@rogierwiertz8522 2 ай бұрын
@@takivilmos You are right. There is no way to directly test if a value in a WeakSet has been garbage collected. An object might get garbage collected when it becomes unreachable, which means there are no references to it. You could indirectly check if the value has been garbage collected: let son = { name: 'Alex' }; const daughter = { name: 'Bree' } const weakSon = new WeakRef(son); // Create a weak reference to the son object const kids = new WeakSet(); kids.add(son); kids.add(daughter); son = null; // No more strong references to the previous value of son const interval = setInterval(() => { if (!kids.has(weakSon.deref())) { console.log('Son has been garbage collected'); return clearInterval(interval); } console.log('Waiting for garbage collection...'); for (let i = 0; i < 10; i++) { // Allocate memory to trigger garbage collection const largeObj = new Array(1000000).fill('x'); kids.add(largeObj); } }, 1000);
Introduction to JS structuredClone
12:17
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 1,6 М.
Master Async JavaScript: What it is and How to Use it
1:18:37
Sheryians Coding School
Рет қаралды 197 М.
Please be kind🙏
00:34
ISSEI / いっせい
Рет қаралды 167 МЛН
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 38 МЛН
Homemade Professional Spy Trick To Unlock A Phone 🔍
00:55
Crafty Champions
Рет қаралды 57 МЛН
1❤️#thankyou #shorts
00:21
あみか部
Рет қаралды 88 МЛН
.NET Framework vs .NET Core vs .NET vs .NET Standard vs C#
25:14
IAmTimCorey
Рет қаралды 540 М.
Weak JavaScript - HTTP 203
29:22
Chrome for Developers
Рет қаралды 25 М.
Maps vs. Objects in JavaScript - What's the Difference?
11:48
JavaScript Proxies
22:54
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 11 М.
Complete Guide to JS Sets: How They Work & When To Use Them
18:38
Chrome Dev Tools Data and File Storage
14:17
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 2,3 М.
Урок 13. JavaScript. Все о Map, Set, WeakMap, WeakSet с примерами
40:03
Владилен Минин
Рет қаралды 150 М.
The Difference between JS Expressions and Statements
8:45
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 6 М.
WeakMaps - HTTP203
6:31
Chrome for Developers
Рет қаралды 19 М.
Please be kind🙏
00:34
ISSEI / いっせい
Рет қаралды 167 МЛН