We’ve recently added support for the FinalizationRegistry API in Cloudflare Workers. This API allows developers to request a callback when a JavaScript object is garbage-collected, a feature that can be particularly relevant for managing external resources, such as memory allocated by WebAssembly (Wasm). However, despite its availability, our general advice is: avoid using it directly in most scenarios.Our decision to add FinalizationRegistry — while still cautioning against using it — opens up a bigger conversation: how memory management works when JavaScript and WebAssembly share the same runtime. This is becoming more common in high-performance web apps, and getting it wrong can lead to memory leaks, out-of-memory errors, and performance issues, especially in resource-constrained environments like Cloudflare Workers.In this post, we’ll look at how JavaScript and Wasm handle memory differently, why that difference matters, and what FinalizationRegistry is actually useful for. We’ll also explain its limitations, particularly around timing and predictability, walk through why we decided to support it, and how we’ve made it safer to use. Finally, we’ll talk about how newer JavaScript language features offer a more reliable and structured approach to solving these problems.
pull down to refresh
related posts
0 sats \ 0 replies \ @021f3af1a6 11 Jun
It’s kind of ironic — Cloudflare adds FinalizationRegistry support just to tell us not to use it 😂. But I get it. Relying on unpredictable GC behavior for cleanup in performance-critical environments like Workers sounds like a recipe for subtle bugs.
The bigger takeaway for me: just because a tool exists in JavaScript doesn’t mean it’s safe to use everywhere. Better to use structured cleanup patterns when dealing with Wasm or tight memory constraints.
Anyone here actually used FinalizationRegistry successfully in production?
reply