"garbage collection only happens when the browser thinks it has downtime and only when it's under memory stress" We solved it. That's why chrome uses so much ram.
14:55: You can use primitive wrapper objects. new WeakRef(new String("hi")) works, since the String constructor returns a String wrapper object. In fact, primitives are automatically upgraded to a wrapper object whenever methods are called on them, since primitives don't have prototypes. Primitive wrappers also get downgraded to primitives automatically in some cases. As for equality, note that (new String("hi")) !== (new String("hi")), but "hi" === "hi". Primitive strings aren't equal because they are "identical", but because the ECMAScript spec says they are equal. Section 7.2.13 says strings are equal if "x and y are exactly the same sequence of code units (same length and same code units at corresponding indices)". (although idk how they are actually implement any of this) Primitive wrapper objects are somewhat interesting but not very useful, except for niche use cases like this.
Main takeaway from the video: you can optionally chain function invocations with `?.()`. But seriously, the video was super interesting. I wonder what happens if a FinalizationRegistry holds a reference to itself and gets garbage collected... this breaks my brain. Missing the update on the string pool though ;)
Yeah string pool thing update is I am curious about. Cause that is the most confusing part in this video. Premitive types also should get garbage collected don't they??
technikhil Primitive values as I understand are stored in the stack and not the heap. So if you declared a Boolean inside a function call, that Boolean would be popped off the stack and would no longer exist once the function call had ended. This doesn’t address what happens with strings or closures and I don’t know enough about that yet to offer any explanation.
@@mysterbirdtrooper6516 yeah sorry I meant by primitives in closure scope only. I guess new let and const might be making some change in how they are handled.
Love these ... being stuck home at COVID ... I don't get the chance to have these discussions and it's great having Surma and Jake go back and forth on this advanced topic is great!
I had no idea that a weak reference was even a term. It sounds like what happens when you create an array in a C++ function and then return it from that function, but you get a compiler warning that the array is not guaranteed to exist later on when you need it
Java has it explicitly as well for example. The idea is just that you can hold a reference to something, but that reference doesn't prevent GC from removing the object. For cases where you want to work with objects as long as they're available and thereby need a reference, but don't want that reference to prolong the object lifespan.
I definitely can appreciate the correction about why they're not iterable _before_ you explained your current understanding! Lots of people correct things like that after you've internalized the information, and it can be really difficult to undo it in your mind.
21:49 Shouldn’t he token be returned by `registry.register()`? It seems a bit extra to have to care about the actual implementation of the token; looks like it really can be any unique value (ideally, a symbol, but that’s not required).
I asked one of the proposal champions what the reasoning is: “design-wise, the ability to pass in a key lets you group multiple registrations with a single key, which is more powerful. performance-wise, having a key makes things slower since we need to track it in a map, so being optional is nice.”
If one of the motivations for WeakRefs/FinalizationRegistry is to allow garbage collection in one context to trigger reclamation of memory in another context, e.g. in WebAssembly or a Worker (via Comlink, say), then are browsers likely to invoke garbage collection in one context when a related but distinct context is under memory pressure? If I have large WebAssembly objects associated with small Javascript objects that become unreferenced, I'm wondering how often this will actually help. I know this is likely implementation-specific, but what do current implementations do?
I don't think “memory pressure” is determined per realm (or scope). The entire system is under memory pressure and that will trigger garbage collection. That will cause your small JS object to get GC’d and that in turn will trigger your code to free up Wasm memory. That being said, Wasm memory is special. It can only grow, not shrink, so if anything you are preventing _further_ allocations instead of reducing occupied memory.
@Surma Why do I have to pass in the unregistertoken? Can it not be on lines of settimeout where settimeout returns me the token to in turn pass to clearTimeout? Am I missing something?
6:27 In the second block, is an assignment to a key with the value of profile `profile` with `auxiliaryData.set` implied/missing before use of `auxiliaryData.get` on the last line?
technikhil yeah, but I think I need to see a concrete app to that uses it to see the point - and also it doesn’t answer why such a generic API was developed for such a specific problem
That basically allow an arbitrary class(or object) have deconstructor without any code that call deinit function. You can do that manually, but when is an object not needed(will be GC) is not easy to tell, since things passes around and it can be circular. That's an excellent feature, that won't be adopted unless everyone say yes.
"garbage collection only happens when the browser thinks it has downtime and only when it's under memory stress"
We solved it. That's why chrome uses so much ram.
Chrome really eats up RAM.
14:55: You can use primitive wrapper objects. new WeakRef(new String("hi")) works, since the String constructor returns a String wrapper object. In fact, primitives are automatically upgraded to a wrapper object whenever methods are called on them, since primitives don't have prototypes. Primitive wrappers also get downgraded to primitives automatically in some cases.
As for equality, note that (new String("hi")) !== (new String("hi")), but "hi" === "hi". Primitive strings aren't equal because they are "identical", but because the ECMAScript spec says they are equal. Section 7.2.13 says strings are equal if "x and y are exactly the same sequence of code units (same length and same code units at corresponding indices)". (although idk how they are actually implement any of this)
Primitive wrapper objects are somewhat interesting but not very useful, except for niche use cases like this.
"Gone But not Forgotten" 💫
Who's gone?
What happened to Paul Lewis?
@@seanpheneger6632 I don't know 😂
@@seanpheneger6632 Nothing. He used to be in HTTP 203 few years ago. ATM he's part of the DevTools team.
Tbh, I love these videos.
Coz half the time I have no idea what they are talking about
and most the time I am like - uh? What? Come again?
Main takeaway from the video: you can optionally chain function invocations with `?.()`. But seriously, the video was super interesting. I wonder what happens if a FinalizationRegistry holds a reference to itself and gets garbage collected... this breaks my brain. Missing the update on the string pool though ;)
Yeah string pool thing update is I am curious about. Cause that is the most confusing part in this video. Premitive types also should get garbage collected don't they??
technikhil Primitive values as I understand are stored in the stack and not the heap. So if you declared a Boolean inside a function call, that Boolean would be popped off the stack and would no longer exist once the function call had ended.
This doesn’t address what happens with strings or closures and I don’t know enough about that yet to offer any explanation.
@@mysterbirdtrooper6516 yeah sorry I meant by primitives in closure scope only. I guess new let and const might be making some change in how they are handled.
Here is another use case ua-cam.com/video/TPm-UhWkiq8/v-deo.html
wooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooow
Love these ... being stuck home at COVID ... I don't get the chance to have these discussions and it's great having Surma and Jake go back and forth on this advanced topic is great!
00:50 Exactly you are right this is the best part 😂😂😂😂
I had no idea that a weak reference was even a term. It sounds like what happens when you create an array in a C++ function and then return it from that function, but you get a compiler warning that the array is not guaranteed to exist later on when you need it
Java has it explicitly as well for example. The idea is just that you can hold a reference to something, but that reference doesn't prevent GC from removing the object. For cases where you want to work with objects as long as they're available and thereby need a reference, but don't want that reference to prolong the object lifespan.
Okay so quantum references.
for a comment made by "ihavegotnoidea" this seems fairly accurate
I definitely can appreciate the correction about why they're not iterable _before_ you explained your current understanding! Lots of people correct things like that after you've internalized the information, and it can be really difficult to undo it in your mind.
Can we take this information authoritatively?
21:49 Shouldn’t he token be returned by `registry.register()`? It seems a bit extra to have to care about the actual implementation of the token; looks like it really can be any unique value (ideally, a symbol, but that’s not required).
I asked one of the proposal champions what the reasoning is: “design-wise, the ability to pass in a key lets you group multiple registrations with a single key, which is more powerful.
performance-wise, having a key makes things slower since we need to track it in a map, so being optional is nice.”
I'm having trouble keeping up with these topics lately 😅
If one of the motivations for WeakRefs/FinalizationRegistry is to allow garbage collection in one context to trigger reclamation of memory in another context, e.g. in WebAssembly or a Worker (via Comlink, say), then are browsers likely to invoke garbage collection in one context when a related but distinct context is under memory pressure? If I have large WebAssembly objects associated with small Javascript objects that become unreferenced, I'm wondering how often this will actually help. I know this is likely implementation-specific, but what do current implementations do?
I don't think “memory pressure” is determined per realm (or scope). The entire system is under memory pressure and that will trigger garbage collection. That will cause your small JS object to get GC’d and that in turn will trigger your code to free up Wasm memory. That being said, Wasm memory is special. It can only grow, not shrink, so if anything you are preventing _further_ allocations instead of reducing occupied memory.
@Surma Why do I have to pass in the unregistertoken? Can it not be on lines of settimeout where settimeout returns me the token to in turn pass to clearTimeout? Am I missing something?
Hey Surma, can we get a superCharged episode, where you build yourself a teleprompter for HTTP 203
Live for this.
This is AWSOME!!
sUUper aDVanCE stAFf
6:27
In the second block, is an assignment to a key with the value of profile `profile` with `auxiliaryData.set` implied/missing before use of `auxiliaryData.get` on the last line?
Yeah maybe, but I'm using optional chaining so the code doesn't break either way :9
@@Chill157 yes
but can we get it free noooooooooooooooo
I think it's "non-compliant"
some weird audio cuts at 17:58
ok im proper confused now
Not convinced as to why this is useful - maybe I should read up on the use case for comlink
I guess surma mentioned that there are strong usecases in webassembly and javascript interoperability. If I am not mistaken.
technikhil yeah, but I think I need to see a concrete app to that uses it to see the point - and also it doesn’t answer why such a generic API was developed for such a specific problem
@@rlamacraft yeah I am thinking of it too. Let me know if you come up with something and I'll let know if I come with something too. Thanks
That basically allow an arbitrary class(or object) have deconstructor without any code that call deinit function.
You can do that manually, but when is an object not needed(will be GC) is not easy to tell, since things passes around and it can be circular.
That's an excellent feature, that won't be adopted unless everyone say yes.
Bizarro 302. Garbage collection, weak refs, dbl-rAF, isInputPending, rIC, workers...Javascript timing has become the NYC subway system.
hahahahahahaha 🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣