Weak References, Reachability, WeakSets, and WeakMaps

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

Steve Griffith - Prof3ssorSt3v3

Steve Griffith - Prof3ssorSt3v3

Күн бұрын

Пікірлер: 29
@leonardchan2638
@leonardchan2638 Жыл бұрын
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 Жыл бұрын
They're not an everyday thing
@pasej5
@pasej5 10 ай бұрын
You made this so easy to understand thank you.
@a1r592
@a1r592 Жыл бұрын
Thanks for the video, very clear explanation!
@kosnowman
@kosnowman Жыл бұрын
this is amazing thank you Steve!
@mohitpal1505
@mohitpal1505 Жыл бұрын
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?
@ofeenee
@ofeenee Жыл бұрын
Thank you for the great video! 🙏
@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.
@tvili999
@tvili999 8 ай бұрын
​@@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 7 ай бұрын
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 7 ай бұрын
@@tvili999 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);
@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!
@kerodfresenbetgebremedhin1881
@kerodfresenbetgebremedhin1881 Жыл бұрын
stupendous
@aibulat1871
@aibulat1871 2 күн бұрын
for Map -- keys don't have to be Objects. They can be any primitives as well. For WeakMap - keys cannot be primitive types - only reference types.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 2 күн бұрын
@aibulat1871 keys don't have to be objects, they are allowed to be.
@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?
@shahinza
@shahinza Жыл бұрын
Thank you 🙏
@bollywood90ssongs65
@bollywood90ssongs65 20 күн бұрын
it is very confusing and why to use must be cleared out, still trying to understand what is the difference normal use and this one.
@Arcanist1010
@Arcanist1010 Жыл бұрын
May i know what font name you use?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
Jet Brains Mono
Introduction to JS structuredClone
12:17
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 1,8 М.
Deep Dive into Array from method
22:05
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 2,8 М.
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 110 МЛН
Wait for the last one 🤣🤣 #shorts #minecraft
00:28
Cosmo Guy
Рет қаралды 25 МЛН
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 7 МЛН
ЗНАЛИ? ТОЛЬКО ОАЭ 🤫
00:13
Сам себе сушист
Рет қаралды 4 МЛН
Important differences between textContent and innerText
11:44
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 2,5 М.
Maps vs. Objects in JavaScript - What's the Difference?
11:48
Map, Set, WeakMap & WeakSet in JavaScript - Real use case
12:20
You Should Use Maps and Sets in JS
14:28
Syntax
Рет қаралды 10 М.
Map vs Object in JavaScript
14:33
Leigh Halliday
Рет қаралды 21 М.
Chrome Dev Tools Source Panel and Breakpoints
18:21
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 7 М.
Revealing the Differences between HTML Dialogs and the Popover API
24:14
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 9 М.
Map, Filter, Reduce - JavaScript Tutorial for Beginners
23:40
PedroTech
Рет қаралды 10 М.
Async Rust Is A Bad Language | Prime Reacts
28:46
ThePrimeTime
Рет қаралды 105 М.
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 110 МЛН