WeakRef

A WeakRef (ES2021) does not keep its target alive: deref() returns the target, or undefined once collected. After creating a WeakRef or calling deref(), the target survives until the current job ends, so synchronous code never sees it vanish halfway. Node.js 2,131 's --expose-gc flag makes the timing visible:

A weakly held 50 MB object disappears after the job endsJavaScriptLive
// node --expose-gc weakref.mjs
let image = { pixels: new Uint8Array(50_000_000) };
const ref = new WeakRef(image);
image = null;                                   // drop the only strong reference
globalThis.gc();
console.log('same job:', ref.deref()?.pixels.length);  // kept alive until the job ends
await new Promise((resolve) => setTimeout(resolve));
globalThis.gc();
console.log('next job:', ref.deref());