Better HN
topnewbestaskshowjobs
Log inSign up

lele04

1 karma · joined Jul 31, 2024 (2y ago) · 1 submissions

Submissions

comment 2y ago · link · parent

Thanks for the explanation. I think I understand why the leak happens with the problematic case. But why doesn't the last example before the problematic example leak? Doesn't the cleanup closure still retain the demo_env.

This is the example I mean:

  function demo() {
    const bigArrayBuffer = new ArrayBuffer(100_000_000);
    
    const id = setTimeout(() => {
      console.log('hello');
    }, 1000);
   
    return () => clearTimeout(id);
  }
  
  globalThis.cancelDemo = demo();
[Edit]

The blog post got updated with some links to other articles, which made it clear to me now. It looks like variables are only saved in the context/env if they are used in an inner function. Although one of the blog posts says that this is an implementation detail but at least it seems to work like this in FF/SM and V8.