Skip to content

WeakRef tutorial #2831

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

Open
dy opened this issue Jan 16, 2022 · 9 comments
Open

WeakRef tutorial #2831

dy opened this issue Jan 16, 2022 · 9 comments

Comments

@dy
Copy link

dy commented Jan 16, 2022

WeakRefs and FinalizationRegistry are hard to master.
Please add tutorials

@iliakan
Copy link
Member

iliakan commented Jan 21, 2022

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

@dy
Copy link
Author

dy commented Jan 21, 2022

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?

@iliakan
Copy link
Member

iliakan commented Jan 21, 2022

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

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

@iliakan
Copy link
Member

iliakan commented Jan 21, 2022

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

@dy
Copy link
Author

dy commented Jan 21, 2022

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.

@iliakan
Copy link
Member

iliakan commented Jan 22, 2022

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

@dy
Copy link
Author

dy commented Jan 22, 2022

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

@joaquinelio
Copy link
Member

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 :)

@WOLFRIEND
Copy link
Contributor

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: wolfriend.official@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants