en.javascript.info icon indicating copy to clipboard operation
en.javascript.info copied to clipboard

WeakRef tutorial

Open dy opened this issue 4 years ago • 9 comments

WeakRefs and FinalizationRegistry are hard to master. Please add tutorials

dy avatar Jan 16 '22 16:01 dy

What's difficult? What do you fail to understand from the current text?

iliakan avatar Jan 21 '22 10:01 iliakan

Which current text? I wasn't able to find any mention of WeakRef in javascript.info.

What do you fail to understand from the current text?

I can ask same question regarding any article on javascript.info: what do I fail to understand on MDN so that I read javascript.info. I like human-friendly, more detailed and more practical approach here.

In particular, there's not so many good sources on WeakRefs on the web, the main 3 are:

What's missing:

  • good use-cases and examples to build intuition around them. There's only 3 main examples: binding event listeners, caching and iterable WeakMap.
  • practical questions/understanding:
    • when is it better to remove listeners/unsubscribe - when .deref() returns undefined, or in FinalizationRegistry?
    • what's the difference between .deref() === undefined and FinalizationRegistry callback? Is that called at different times?
    • what's possible patterns organizing weak event subscriptions? Wrapping listener into deref-function, as in v8 example, or maybe storing a list of weakrefs? Which one will and which will not be collected by gc?
    • why is it said in the proposal that iterable WeakMap cannot store WeakRef instances as keys, else it won't be collected?
    • what's the right object to use for FinalizationRegistry - event source or event listener?
  • How to test WeakRefs? (turns out there's --expose-gc option for node and global.gc() call, or even more secret --allow-natives-syntax with %CollectGarbage('all').
  • Possible pitfalls, like - why object is not being garbage-collected (soft references in fn contexts) etc.

I think all that and more could be a good help for newcomers if covered in a separate tutorial. Wdyt?

dy avatar Jan 21 '22 17:01 dy

Sorry, you're right, nothing about WeakRefs, indeed.

Personally, I never used them, and don't intend to.

iliakan avatar Jan 21 '22 18:01 iliakan

I'd welcome a chapter about them from anyone who feels he knows enough to write one.

iliakan avatar Jan 21 '22 18:01 iliakan

I suppose it should reside somewhere around garbage-collection, since that's directly related topic.

I never used them, and don't intend to.

I'm curious why not? WeakRefs can significantly simplify API of almost anything - they make removeEventListener, unsubscribe, destroy, dispose etc methods unnecessary. Ie. no more manual memory control needed, without memory leaks. For example, there was Symbol.dispose proposal, which is now elegantly resolved. Imagine jQuery plugin, that gets destroyed and memory collected simply when element is removed from DOM and ref is lost. If you don't deal with building packages/components/frameworks - I guess you don't need it as much. But for shared things that's usability booster.

dy avatar Jan 21 '22 19:01 dy

@dy Maybe ;) One needs to carefully consider it. Right now, you can see a recommendation to avoid it in MDN.

iliakan avatar Jan 22 '22 07:01 iliakan

Yes, unless you master them. Hence the topic :)

dy avatar Jan 22 '22 12:01 dy

I know NOTHING about this...

But I usually avoid tricky things that need to be mastered, shared code or not. Tricky things lead to tricky bugs.

So I agree, the topic should be covered, with the appropiate warnings... ...special focus in the warnings :)

joaquinelio avatar Jan 22 '22 13:01 joaquinelio

Hi, guys. I'd love to write articles about WeakRef and FinalizationRegistry. I have material on this topic that I would be happy to share. In fact, there is not much information about these features or how to use them, but I still have something to write about, and I have a demo that shows how it works. I can write these articles in the following languages: Eng, Rus, Urk. Please let me know how I could contribute and do this? Just in case, here are my contacts: LinkedIn: https://www.linkedin.com/in/aleksandrtkachenko/ Email: [email protected]

WOLFRIEND avatar Oct 08 '23 08:10 WOLFRIEND