[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"article-weakref-finalizationregistry-leak-safe-cache":44,"search-suggestions":248,"path-membership-weakref-finalizationregistry-leak-safe-cache":289,"related-weakref-finalizationregistry-leak-safe-cache":290,"code:js:true:1svhbl":420,"code:js:true:1ccwh22":421,"code:js:true:17xv7ax":422,"code:js:true:i24bal":423,"code:js:true:7trol4":424,"comments-01a1b4aa-fc88-72e4-9a27-437c6ed0bb20":425},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":53,"likeCount":19,"commentCount":19,"author":54,"vertical":59,"topic":60,"tags":63,"_count":75,"playground":77,"body":79,"bodyMd":231,"seo":232,"translationGroupId":235,"series":48,"podcastUrl":48,"verticalId":5,"thread":236,"assessments":238,"translations":244,"quiz":246},"01a1b4aa-fc88-72e4-9a27-437c6ed0bb20","weakref-finalizationregistry-leak-safe-cache","You're Leaking Memory With That Cache. WeakMap Fixes It.",null,"A Map-based cache looks harmless until you check the heap snapshot and find every discarded object still sitting there, held alive by the one `Map` nobody suspected. `WeakMap` fixes the key-as-cache-slot case outright; `WeakRef` and `FinalizationRegistry` cover what's left.","\u002Fmedia\u002Fcovers\u002Fweakref-finalizationregistry-leak-safe-cache.png",6,"2026-11-27T11:40:21.749Z",12,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},"javascript","JavaScript",[64,66,69,72],{"slug":61,"name":65,"color":48},"Javascript",{"slug":67,"name":68,"color":48},"webdev","Webdev",{"slug":70,"name":71,"color":48},"performance","Performance",{"slug":73,"name":74,"color":48},"programming","Programming",{"assessments":76},1,{"slug":46,"title":78},"WeakRef &amp; FinalizationRegistry — leak-safe cache playground",{"blocks":80,"version":76},[81,85,88,94,97,102,105,108,112,115,118,121,124,127,130,134,137,143,146,149,153,156,159,163,166,169,172,175,179,182,186,189,192,195,198,201,204,207,210,213,216,219,222,225],{"id":82,"html":83,"type":84},"b1","\u003Cp>Open the memory profiler on a page that&#39;s supposed to be idle, and the retained-size graph should flatten out. On this one it didn&#39;t. Every navigation added a little more, and none of it ever came back down.\u003C\u002Fp>","paragraph",{"id":86,"html":87,"type":84},"b2","\u003Cp>The suspect wasn&#39;t a leaked event listener or a forgotten \u003Ccode>setInterval\u003C\u002Fcode> — both had already been ruled out. It was a cache. A perfectly reasonable-looking one:\u003C\u002Fp>",{"id":89,"code":90,"type":91,"language":92,"highlight":93},"b3","const metadataCache = new Map();\n\nfunction getMetadata(record) {\n  if (metadataCache.has(record)) return metadataCache.get(record);\n  const computed = expensiveParse(record);\n  metadataCache.set(record, computed);\n  return computed;\n}","code","js",[],{"id":95,"html":96,"type":84},"b4","\u003Cp>\u003Ccode>record\u003C\u002Fcode> objects came from an API response, got rendered, and were thrown away the moment the user navigated elsewhere. Except they weren&#39;t thrown away — not really. \u003Ccode>metadataCache\u003C\u002Fcode> still had a reference to every single one, and a \u003Ccode>Map\u003C\u002Fcode> never lets go of a key on its own. The garbage collector doesn&#39;t get a vote: as long as \u003Cem>something\u003C\u002Fem> holds a strong reference, the object stays on the heap, parse result and all.\u003C\u002Fp>",{"id":98,"html":99,"text":100,"type":101,"level":31},"b5","Why \u003Ccode>Map\u003C\u002Fcode> can&#39;t be trusted with throwaway keys","Why Map can't be trusted with throwaway keys","heading",{"id":103,"html":104,"type":84},"b6","\u003Cp>This isn&#39;t a bug in \u003Ccode>Map\u003C\u002Fcode> — it&#39;s doing exactly what a \u003Ccode>Map\u003C\u002Fcode> is for. A \u003Ccode>Map\u003C\u002Fcode> is built to \u003Cstrong>keep\u003C\u002Fstrong> what you put in it until you explicitly delete it. That&#39;s the correct behavior for a cache keyed by a string or a small fixed set of values. It&#39;s the wrong behavior for a cache keyed by objects whose whole lifecycle is &quot;used once, then gone&quot; — because nothing ever calls \u003Ccode>.delete()\u003C\u002Fcode> for you, and the moment you forget to, the cache becomes a second copy of every object your app ever touched, held open forever.\u003C\u002Fp>",{"id":106,"html":107,"type":84},"b7","\u003Cp>The usual first fix is \u003Ccode>WeakMap\u003C\u002Fcode>:\u003C\u002Fp>",{"id":109,"code":110,"type":91,"language":92,"highlight":111},"b8","const metadataCache = new WeakMap(); \u002F\u002F same calls, weak keys\n\nfunction getMetadata(record) {\n  if (metadataCache.has(record)) return metadataCache.get(record);\n  const computed = expensiveParse(record);\n  metadataCache.set(record, computed);\n  return computed;\n}",[],{"id":113,"html":114,"type":84},"b9","\u003Cp>Same API, one word different, and the leak is gone. A \u003Ccode>WeakMap\u003C\u002Fcode> holds its keys \u003Cem>weakly\u003C\u002Fem>: once nothing else in the program references a given \u003Ccode>record\u003C\u002Fcode>, the engine is free to collect it, and its entry in the \u003Ccode>WeakMap\u003C\u002Fcode> disappears with it — automatically, with no \u003Ccode>.delete()\u003C\u002Fcode> anywhere. \u003Ccode>WeakMap\u003C\u002Fcode> has existed since ES2015, and for &quot;cache keyed by an object, evict when the object is gone,&quot; it&#39;s the entire answer. Reach for it first.\u003C\u002Fp>",{"id":116,"html":117,"type":84},"b10","\u003C!-- playground:start -->",{"id":119,"html":120,"text":120,"type":101,"level":31},"b11","🎮 Try it yourself",{"id":122,"html":123,"type":84},"b12","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweakref-finalizationregistry-leak-safe-cache\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":125,"html":126,"type":84},"b13","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":128,"html":129,"type":84},"b14","\u003C!-- playground:end -->",{"id":131,"html":132,"text":133,"type":101,"level":31},"b15","The case \u003Ccode>WeakMap\u003C\u002Fcode> can&#39;t cover","The case WeakMap can't cover",{"id":135,"html":136,"type":84},"b16","\u003Cp>\u003Ccode>WeakMap\u003C\u002Fcode> only solves the problem when the weak thing is a \u003Cem>map key\u003C\u002Fem>. Two situations fall outside that:\u003C\u002Fp>",{"id":138,"type":139,"items":140,"ordered":18},"b17","list",[141,142],"You need to hold a weak reference \u003Cstrong>by itself\u003C\u002Fstrong> — in a class field, an array entry, a plain object property — not as a \u003Ccode>Map\u003C\u002Fcode> key at all. \u003Ccode>WeakMap\u003C\u002Fcode>\u002F\u003Ccode>WeakSet\u003C\u002Fcode> have no API for that; they only accept objects as keys or set members.","You need to \u003Cstrong>do something\u003C\u002Fstrong> when the object actually gets collected — close a \u003Ccode>WebSocket\u003C\u002Fcode>, cancel a subscription, remove an entry from an unrelated index — and \u003Ccode>WeakMap\u003C\u002Fcode> gives you silence. The entry just vanishes; nothing tells your code it happened.",{"id":144,"html":145,"type":84},"b18","\u003Cp>That&#39;s what the other two ES2021 additions are for.\u003C\u002Fp>",{"id":147,"html":148,"type":84},"b19","\u003Cp>\u003Cstrong>\u003Ccode>WeakRef\u003C\u002Fcode>\u003C\u002Fstrong> wraps a single object in a container you can hold anywhere, without that container keeping the object alive:\u003C\u002Fp>",{"id":150,"code":151,"type":91,"language":92,"highlight":152},"b20","let ref = new WeakRef(record);\n\nref.deref(); \u002F\u002F the record, while it's still alive\nref.deref(); \u002F\u002F undefined, once nothing else references it — could be later in the same function",[],{"id":154,"html":155,"type":84},"b21","\u003Cp>\u003Ccode>.deref()\u003C\u002Fcode> is the only thing you do with it: call it, and either get the live object back or \u003Ccode>undefined\u003C\u002Fcode>. There is no event, no promise, no way to ask &quot;is it gone yet&quot; without calling \u003Ccode>.deref()\u003C\u002Fcode> and checking.\u003C\u002Fp>",{"id":157,"html":158,"type":84},"b22","\u003Cp>\u003Cstrong>\u003Ccode>FinalizationRegistry\u003C\u002Fcode>\u003C\u002Fstrong> is the piece that gives you a callback instead of a poll:\u003C\u002Fp>",{"id":160,"code":161,"type":91,"language":92,"highlight":162},"b23","const registry = new FinalizationRegistry((heldValue) => {\n  console.log(`cleaning up after ${heldValue}`);\n  releaseExternalResource(heldValue);\n});\n\nregistry.register(record, record.id);",[],{"id":164,"html":165,"type":84},"b24","\u003Cp>\u003Ccode>register(target, heldValue, unregisterToken?)\u003C\u002Fcode> watches \u003Ccode>target\u003C\u002Fcode>. Once \u003Ccode>target\u003C\u002Fcode> becomes unreachable and the engine gets around to collecting it, the registry calls your callback with \u003Ccode>heldValue\u003C\u002Fcode> — never with \u003Ccode>target\u003C\u002Fcode> itself, because the callback runs \u003Cem>after\u003C\u002Fem> \u003Ccode>target\u003C\u002Fcode> is already gone. That&#39;s also why \u003Ccode>target\u003C\u002Fcode> and \u003Ccode>heldValue\u003C\u002Fcode> can&#39;t be the same object: pass the same reference for both and \u003Ccode>register()\u003C\u002Fcode> throws a \u003Ccode>TypeError\u003C\u002Fcode>, because the registry holding \u003Ccode>heldValue\u003C\u002Fcode> strongly would defeat the entire point by keeping \u003Ccode>target\u003C\u002Fcode> alive forever.\u003C\u002Fp>",{"id":167,"html":168,"type":84},"b25","\u003Cp>If you dispose of something yourself before GC gets to it, call \u003Ccode>registry.unregister(unregisterToken)\u003C\u002Fcode> — a third argument to \u003Ccode>register()\u003C\u002Fcode> — so the callback doesn&#39;t fire twice.\u003C\u002Fp>",{"id":170,"html":171,"text":171,"type":101,"level":31},"b26","Building a cache that cleans up after itself",{"id":173,"html":174,"type":84},"b27","\u003Cp>Put the two together and the parse-result cache gets an upgrade \u003Ccode>WeakMap\u003C\u002Fcode> alone couldn&#39;t give it: logging (or releasing an external resource) the moment an entry is actually swept.\u003C\u002Fp>",{"id":176,"code":177,"type":91,"language":92,"highlight":178},"b28","const metadataCache = new WeakMap();\nconst cleanupLog = new FinalizationRegistry((id) => {\n  console.log(`metadata for record ${id} was collected`);\n});\n\nfunction getMetadata(record) {\n  if (metadataCache.has(record)) return metadataCache.get(record);\n  const computed = expensiveParse(record);\n  metadataCache.set(record, computed);\n  cleanupLog.register(record, record.id);\n  return computed;\n}",[],{"id":180,"html":181,"type":84},"b29","\u003Cp>The \u003Ccode>WeakMap\u003C\u002Fcode> still does the actual weak-caching — it&#39;s the right tool for that half. The \u003Ccode>FinalizationRegistry\u003C\u002Fcode> is bolted on purely for visibility and side effects: if \u003Ccode>getMetadata\u003C\u002Fcode> also opened a \u003Ccode>BroadcastChannel\u003C\u002Fcode> per record, this is where you&#39;d close it, instead of hoping the caller remembers to.\u003C\u002Fp>",{"id":183,"html":184,"text":185,"type":101,"level":31},"b30","The guarantee you don&#39;t get — and why that&#39;s fine","The guarantee you don't get — and why that's fine",{"id":187,"html":188,"type":84},"b31","\u003Cp>Here&#39;s the part that trips people up the first time they read the spec: \u003Cstrong>there is no guarantee a \u003Ccode>FinalizationRegistry\u003C\u002Fcode> callback ever runs.\u003C\u002Fstrong> Not &quot;runs eventually&quot; — possibly never. If the page unloads first, if the engine decides the cleanup isn&#39;t worth the cycles, if the object happens to never get collected during the page&#39;s lifetime — the callback is simply skipped. ECMA-262 says this on purpose, because it leaves engines free to optimize garbage collection however they want without being locked into a cleanup contract.\u003C\u002Fp>",{"id":190,"html":191,"type":84},"b32","\u003Cp>That single sentence should decide every use of these two APIs. \u003Ccode>WeakRef\u003C\u002Fcode> and \u003Ccode>FinalizationRegistry\u003C\u002Fcode> are for \u003Cstrong>optimization and housekeeping\u003C\u002Fstrong> — a cache that&#39;s allowed to forget things, a debug log, releasing a resource a little sooner than it otherwise would be. They are the wrong tool for anything that must happen: closing a file handle your app&#39;s correctness depends on, flushing data the user will notice if it&#39;s lost, releasing a lock another process is waiting on. For deterministic cleanup, reach for an explicit \u003Ccode>dispose()\u003C\u002Fcode>\u002F\u003Ccode>close()\u003C\u002Fcode> call (or \u003Ccode>using\u003C\u002Fcode>, if your runtime supports resource management) — something that runs because your code said so, not because the garbage collector eventually agreed to.\u003C\u002Fp>",{"id":193,"html":194,"text":194,"type":101,"level":31},"b33","What actually changed",{"id":196,"html":197,"type":84},"b34","\u003Cp>\u003Ccode>WeakMap\u003C\u002Fcode> and \u003Ccode>WeakSet\u003C\u002Fcode> have quietly prevented this class of leak since 2015; most caches never needed anything newer. \u003Ccode>WeakRef\u003C\u002Fcode> and \u003Ccode>FinalizationRegistry\u003C\u002Fcode> — both ES2021, both supported in every evergreen browser and in Node since 14.6 — only earn their place when the weak reference isn&#39;t a map key, or when something genuinely needs to happen on collection rather than just disappear quietly.\u003C\u002Fp>",{"id":199,"html":200,"type":84},"b35","\u003Cp>The fix for the original leak, in the end, was one word: \u003Ccode>Map\u003C\u002Fcode> became \u003Ccode>WeakMap\u003C\u002Fcode>. The heap snapshot flattened out on the next deploy. But the question worth sitting with is the one that found it in the first place — next time a retained-size graph only ever goes up, which cache in your code is the one still holding the door open?\u003C\u002Fp>",{"id":202,"html":203,"type":84},"b36","\u003C!-- quiz:start -->",{"id":205,"html":206,"text":206,"type":101,"level":31},"b37","🧠 Test yourself",{"id":208,"html":209,"type":84},"b38","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweakref-finalizationregistry-leak-safe-cache\u002Fquiz\">Take the 7-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":211,"html":212,"type":84},"b39","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":214,"html":215,"type":84},"b40","\u003C!-- quiz:end -->",{"id":217,"type":218},"b41","divider",{"id":220,"html":221,"type":84},"b42","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":223,"html":224,"type":84},"b43","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":226,"type":139,"items":227,"ordered":18},"b44",[228,229,230],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","Open the memory profiler on a page that's supposed to be idle, and the retained-size graph should flatten out. On this one it didn't. Every navigation added a little more, and none of it ever came back down.\n\nThe suspect wasn't a leaked event listener or a forgotten `setInterval` — both had already been ruled out. It was a cache. A perfectly reasonable-looking one:\n\n```js\nconst metadataCache = new Map();\n\nfunction getMetadata(record) {\n  if (metadataCache.has(record)) return metadataCache.get(record);\n  const computed = expensiveParse(record);\n  metadataCache.set(record, computed);\n  return computed;\n}\n```\n\n`record` objects came from an API response, got rendered, and were thrown away the moment the user navigated elsewhere. Except they weren't thrown away — not really. `metadataCache` still had a reference to every single one, and a `Map` never lets go of a key on its own. The garbage collector doesn't get a vote: as long as *something* holds a strong reference, the object stays on the heap, parse result and all.\n\n## Why `Map` can't be trusted with throwaway keys\n\nThis isn't a bug in `Map` — it's doing exactly what a `Map` is for. A `Map` is built to **keep** what you put in it until you explicitly delete it. That's the correct behavior for a cache keyed by a string or a small fixed set of values. It's the wrong behavior for a cache keyed by objects whose whole lifecycle is \"used once, then gone\" — because nothing ever calls `.delete()` for you, and the moment you forget to, the cache becomes a second copy of every object your app ever touched, held open forever.\n\nThe usual first fix is `WeakMap`:\n\n```js\nconst metadataCache = new WeakMap(); \u002F\u002F same calls, weak keys\n\nfunction getMetadata(record) {\n  if (metadataCache.has(record)) return metadataCache.get(record);\n  const computed = expensiveParse(record);\n  metadataCache.set(record, computed);\n  return computed;\n}\n```\n\nSame API, one word different, and the leak is gone. A `WeakMap` holds its keys *weakly*: once nothing else in the program references a given `record`, the engine is free to collect it, and its entry in the `WeakMap` disappears with it — automatically, with no `.delete()` anywhere. `WeakMap` has existed since ES2015, and for \"cache keyed by an object, evict when the object is gone,\" it's the entire answer. Reach for it first.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweakref-finalizationregistry-leak-safe-cache\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\n## The case `WeakMap` can't cover\n\n`WeakMap` only solves the problem when the weak thing is a *map key*. Two situations fall outside that:\n\n- You need to hold a weak reference **by itself** — in a class field, an array entry, a plain object property — not as a `Map` key at all. `WeakMap`\u002F`WeakSet` have no API for that; they only accept objects as keys or set members.\n- You need to **do something** when the object actually gets collected — close a `WebSocket`, cancel a subscription, remove an entry from an unrelated index — and `WeakMap` gives you silence. The entry just vanishes; nothing tells your code it happened.\n\nThat's what the other two ES2021 additions are for.\n\n**`WeakRef`** wraps a single object in a container you can hold anywhere, without that container keeping the object alive:\n\n```js\nlet ref = new WeakRef(record);\n\nref.deref(); \u002F\u002F the record, while it's still alive\nref.deref(); \u002F\u002F undefined, once nothing else references it — could be later in the same function\n```\n\n`.deref()` is the only thing you do with it: call it, and either get the live object back or `undefined`. There is no event, no promise, no way to ask \"is it gone yet\" without calling `.deref()` and checking.\n\n**`FinalizationRegistry`** is the piece that gives you a callback instead of a poll:\n\n```js\nconst registry = new FinalizationRegistry((heldValue) => {\n  console.log(`cleaning up after ${heldValue}`);\n  releaseExternalResource(heldValue);\n});\n\nregistry.register(record, record.id);\n```\n\n`register(target, heldValue, unregisterToken?)` watches `target`. Once `target` becomes unreachable and the engine gets around to collecting it, the registry calls your callback with `heldValue` — never with `target` itself, because the callback runs *after* `target` is already gone. That's also why `target` and `heldValue` can't be the same object: pass the same reference for both and `register()` throws a `TypeError`, because the registry holding `heldValue` strongly would defeat the entire point by keeping `target` alive forever.\n\nIf you dispose of something yourself before GC gets to it, call `registry.unregister(unregisterToken)` — a third argument to `register()` — so the callback doesn't fire twice.\n\n## Building a cache that cleans up after itself\n\nPut the two together and the parse-result cache gets an upgrade `WeakMap` alone couldn't give it: logging (or releasing an external resource) the moment an entry is actually swept.\n\n```js\nconst metadataCache = new WeakMap();\nconst cleanupLog = new FinalizationRegistry((id) => {\n  console.log(`metadata for record ${id} was collected`);\n});\n\nfunction getMetadata(record) {\n  if (metadataCache.has(record)) return metadataCache.get(record);\n  const computed = expensiveParse(record);\n  metadataCache.set(record, computed);\n  cleanupLog.register(record, record.id);\n  return computed;\n}\n```\n\nThe `WeakMap` still does the actual weak-caching — it's the right tool for that half. The `FinalizationRegistry` is bolted on purely for visibility and side effects: if `getMetadata` also opened a `BroadcastChannel` per record, this is where you'd close it, instead of hoping the caller remembers to.\n\n## The guarantee you don't get — and why that's fine\n\nHere's the part that trips people up the first time they read the spec: **there is no guarantee a `FinalizationRegistry` callback ever runs.** Not \"runs eventually\" — possibly never. If the page unloads first, if the engine decides the cleanup isn't worth the cycles, if the object happens to never get collected during the page's lifetime — the callback is simply skipped. ECMA-262 says this on purpose, because it leaves engines free to optimize garbage collection however they want without being locked into a cleanup contract.\n\nThat single sentence should decide every use of these two APIs. `WeakRef` and `FinalizationRegistry` are for **optimization and housekeeping** — a cache that's allowed to forget things, a debug log, releasing a resource a little sooner than it otherwise would be. They are the wrong tool for anything that must happen: closing a file handle your app's correctness depends on, flushing data the user will notice if it's lost, releasing a lock another process is waiting on. For deterministic cleanup, reach for an explicit `dispose()`\u002F`close()` call (or `using`, if your runtime supports resource management) — something that runs because your code said so, not because the garbage collector eventually agreed to.\n\n## What actually changed\n\n`WeakMap` and `WeakSet` have quietly prevented this class of leak since 2015; most caches never needed anything newer. `WeakRef` and `FinalizationRegistry` — both ES2021, both supported in every evergreen browser and in Node since 14.6 — only earn their place when the weak reference isn't a map key, or when something genuinely needs to happen on collection rather than just disappear quietly.\n\nThe fix for the original leak, in the end, was one word: `Map` became `WeakMap`. The heap snapshot flattened out on the next deploy. But the question worth sitting with is the one that found it in the first place — next time a retained-size graph only ever goes up, which cache in your code is the one still holding the door open?\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 7-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweakref-finalizationregistry-leak-safe-cache\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":47,"canonical":233,"description":234},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweakref-finalizationregistry-leak-safe-cache","A Map-based cache looks harmless until you check the heap snapshot and find every discarded object still sitting there, held alive by the one `Map` nobody suspected. `WeakMap` fixe","01a1b4aa-fc88-72e4-9a27-47dc7f6ce5f5",{"id":237,"locked":18},"01a1b4aa-fcf7-7632-aca4-6846bcbb98d3",[239],{"id":240,"slug":46,"title":241,"_count":242},"01a1b4aa-fd21-761b-a81e-58dc77665ebc","WeakRef & FinalizationRegistry — leak-safe caching",{"questions":243},7,[245],{"locale":13,"slug":46},{"id":240,"slug":46,"title":241,"_count":247,"questionCount":243},{"questions":243},[249,251,253,257,261,265,269,271,273,277,281,285],{"slug":67,"name":68,"articles":250},195,{"slug":61,"name":65,"articles":252},156,{"slug":254,"name":255,"articles":256},"frontend","Frontend",103,{"slug":258,"name":259,"articles":260},"tutorial","Tutorial",85,{"slug":262,"name":263,"articles":264},"css","Css",56,{"slug":266,"name":267,"articles":268},"typescript","Typescript",36,{"slug":70,"name":71,"articles":270},33,{"slug":73,"name":74,"articles":272},26,{"slug":274,"name":275,"articles":276},"react","React",23,{"slug":278,"name":279,"articles":280},"node","Node",19,{"slug":282,"name":283,"articles":284},"browser","Browser",18,{"slug":286,"name":287,"articles":288},"nextjs","Nextjs",13,[],{"items":291,"meta":419},[292,310,329,347,364,383,401],{"id":293,"slug":294,"title":295,"subtitle":48,"excerpt":296,"coverUrl":297,"locale":13,"readingMinutes":51,"publishedAt":298,"viewCount":299,"likeCount":19,"commentCount":19,"author":300,"vertical":301,"topic":302,"tags":303,"_count":308,"playground":309,"hasQuiz":17,"hasPlayground":17},"01a1fce0-de91-712e-bc98-72dba2b10bf5","react-use-api-promises-context","`use()` Breaks the Rules of Hooks on Purpose. Here's the Catch.","React's `use()` reads a promise or context mid-render and skips the Rules of Hooks on purpose — but that same freedom hides a caching trap that resuspends your component forever if you miss it.","\u002Fmedia\u002Fcovers\u002Freact-use-api-promises-context.png","2026-11-27T11:49:31.813Z",25,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":274,"name":275},[304,305,306,307],{"slug":274,"name":275,"color":48},{"slug":61,"name":65,"color":48},{"slug":67,"name":68,"color":48},{"slug":254,"name":255,"color":48},{"assessments":76},{"slug":294},{"id":311,"slug":312,"title":313,"subtitle":48,"excerpt":314,"coverUrl":315,"locale":13,"readingMinutes":51,"publishedAt":316,"viewCount":317,"likeCount":19,"commentCount":19,"author":318,"vertical":319,"topic":320,"tags":322,"_count":327,"playground":328,"hasQuiz":17,"hasPlayground":17},"01a1fce0-dac8-71e2-bd25-94b1cc5aa337","typescript-template-literal-types","Template Literal Types Explained: Catching Path Typos at Compile Time","A typo'd dot-path like \"theme.colros.primary\" compiles clean and fails silently at runtime — template literal types make TypeScript catch it before you ship.","\u002Fmedia\u002Fcovers\u002Ftypescript-template-literal-types.png","2026-11-27T11:49:01.332Z",27,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":266,"name":321},"TypeScript",[323,324,325,326],{"slug":266,"name":267,"color":48},{"slug":61,"name":65,"color":48},{"slug":67,"name":68,"color":48},{"slug":258,"name":259,"color":48},{"assessments":76},{"slug":312},{"id":330,"slug":331,"title":332,"subtitle":48,"excerpt":333,"coverUrl":334,"locale":13,"readingMinutes":243,"publishedAt":335,"viewCount":336,"likeCount":19,"commentCount":19,"author":337,"vertical":338,"topic":339,"tags":340,"_count":345,"playground":346,"hasQuiz":17,"hasPlayground":17},"01a1fce0-d4f1-7184-bf57-f7ad669d6255","websocket-reconnect-exponential-backoff","Your WebSocket Reconnects Fine. The Messages It Missed Never Do.","Exponential backoff fixes the reconnect storm. It does nothing for the messages your server sent while the socket was down — those need an app-level replay, or they're gone for good.","\u002Fmedia\u002Fcovers\u002Fwebsocket-reconnect-exponential-backoff.png","2026-11-27T11:48:00.443Z",16,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[341,342,343,344],{"slug":61,"name":65,"color":48},{"slug":67,"name":68,"color":48},{"slug":73,"name":74,"color":48},{"slug":254,"name":255,"color":48},{"assessments":76},{"slug":331},{"id":348,"slug":349,"title":350,"subtitle":48,"excerpt":351,"coverUrl":352,"locale":13,"readingMinutes":336,"publishedAt":353,"viewCount":299,"likeCount":19,"commentCount":19,"author":354,"vertical":355,"topic":356,"tags":357,"_count":362,"playground":363,"hasQuiz":17,"hasPlayground":17},"01a1fce0-ce76-7424-b4da-6db21c69992c","nextjs-weekly-use-cache-remote-vs-private","Next.js 16 use cache: remote vs use cache: private","Next.js 16's use cache: remote shares a cache across instances; use cache: private never leaves one request. Which to pick, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnextjs-weekly-use-cache-remote-vs-private.png","2026-11-27T11:47:29.944Z",{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":286,"name":287},[358,359,360,361],{"slug":286,"name":287,"color":48},{"slug":67,"name":68,"color":48},{"slug":258,"name":259,"color":48},{"slug":61,"name":65,"color":48},{"assessments":76},{"slug":349},{"id":365,"slug":366,"title":367,"subtitle":48,"excerpt":368,"coverUrl":369,"locale":13,"readingMinutes":370,"publishedAt":371,"viewCount":372,"likeCount":19,"commentCount":19,"author":373,"vertical":374,"topic":375,"tags":376,"_count":381,"playground":382,"hasQuiz":17,"hasPlayground":17},"01a1fce0-c9cc-7004-b075-6c0ff7c7b600","weekly-typescript-type-narrowing","TypeScript Type Narrowing: The Complete Guide","Learn how TypeScript type narrowing really works — typeof, instanceof, discriminated unions, and custom type guards — plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fweekly-typescript-type-narrowing.png",15,"2026-11-27T11:46:59.418Z",24,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":266,"name":321},[377,378,379,380],{"slug":266,"name":267,"color":48},{"slug":61,"name":65,"color":48},{"slug":258,"name":259,"color":48},{"slug":67,"name":68,"color":48},{"assessments":76},{"slug":366},{"id":384,"slug":385,"title":386,"subtitle":48,"excerpt":387,"coverUrl":388,"locale":13,"readingMinutes":389,"publishedAt":390,"viewCount":53,"likeCount":19,"commentCount":19,"author":391,"vertical":392,"topic":393,"tags":394,"_count":399,"playground":400,"hasQuiz":17,"hasPlayground":17},"01a1fce0-bedd-7648-b22e-4b7afd1cce13","typescript-branded-types-nominal-typing","TypeScript Can't Tell userId From orderId. Branded Types Can.","TypeScript's structural typing treats userId and orderId as the same type if both are `string` — branded types make them incompatible at compile time.","\u002Fmedia\u002Fcovers\u002Ftypescript-branded-types-nominal-typing.png",5,"2026-11-27T11:45:58.086Z",{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":266,"name":321},[395,396,397,398],{"slug":266,"name":267,"color":48},{"slug":61,"name":65,"color":48},{"slug":67,"name":68,"color":48},{"slug":73,"name":74,"color":48},{"assessments":76},{"slug":385},{"id":402,"slug":403,"title":404,"subtitle":48,"excerpt":405,"coverUrl":406,"locale":13,"readingMinutes":389,"publishedAt":407,"viewCount":408,"likeCount":19,"commentCount":19,"author":409,"vertical":410,"topic":411,"tags":412,"_count":417,"playground":418,"hasQuiz":17,"hasPlayground":17},"01a1df1b-32db-7638-a86f-5cea99530919","typescript-noinfer-generic-widening","A Typo Widened My Generic Instead of Erroring. NoInfer\u003CT> Fixes It.","Why a typo'd default argument sailed through a generic function with zero errors — and how TypeScript's NoInfer\u003CT> stops it from silently widening the inferred type.","\u002Fmedia\u002Fcovers\u002Ftypescript-noinfer-generic-widening.png","2026-11-27T11:45:27.674Z",11,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":266,"name":321},[413,414,415,416],{"slug":266,"name":267,"color":48},{"slug":61,"name":65,"color":48},{"slug":73,"name":74,"color":48},{"slug":67,"name":68,"color":48},{"assessments":76},{"slug":403},{"page":76,"perPage":243,"total":252,"totalPages":276},"\u003Cdiv class=\"shj shj-lang-js shj-multiline\" data-lang=\"js\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> metadataCache \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Map\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\n\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-func\">getMetadata\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">has\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">get\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> computed \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">expensiveParse\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">set\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> computed\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> computed;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-js shj-multiline\" data-lang=\"js\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> metadataCache \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">WeakMap\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>; \u003Cspan class=\"shj-cmnt\">\u002F\u002F same calls, weak keys\n\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-func\">getMetadata\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">has\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">get\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> computed \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">expensiveParse\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">set\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> computed\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> computed;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-js shj-multiline\" data-lang=\"js\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">let\u003C\u002Fspan> ref \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">WeakRef\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\nref\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">deref\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>; \u003Cspan class=\"shj-cmnt\">\u002F\u002F the record, while it's still alive\n\u003C\u002Fspan>ref\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">deref\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>; \u003Cspan class=\"shj-cmnt\">\u002F\u002F undefined, once nothing else references it — could be later in the same function\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-js shj-multiline\" data-lang=\"js\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> registry \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">FinalizationRegistry\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>heldValue\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  console\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">log\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">`cleaning up after \u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">${\u003C\u002Fspan>heldValue\u003Cspan class=\"shj-kwd\">}\u003C\u002Fspan>\u003Cspan class=\"shj-str\">`\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-func\">releaseExternalResource\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>heldValue\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\nregistry\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">register\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> record\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>id\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-js shj-multiline\" data-lang=\"js\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> metadataCache \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">WeakMap\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> cleanupLog \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">FinalizationRegistry\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>id\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  console\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">log\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">`metadata for record \u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">${\u003C\u002Fspan>id\u003Cspan class=\"shj-kwd\">}\u003C\u002Fspan>\u003Cspan class=\"shj-str\"> was collected`\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\n\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-func\">getMetadata\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">has\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">get\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> computed \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">expensiveParse\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  metadataCache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">set\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> computed\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  cleanupLog\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">register\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>record\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> record\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>id\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> computed;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>",{"locked":18,"total":19,"comments":426},[]]