Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/content/changelog/workers/2025-04-24-finalization-registry.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Improved Memory Efficiency for WebAssembly Workers
description: With the introduction of FinalizationRegistry in Workers Runtime, toolchains like Emscripten and wasm-bindgen can automatically reclaim unused WebAssembly heap and reduce memory leaks.
products:
- workers
date: 2025-05-24T00:00:00Z
---

[FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) is now available in Workers (opt-in with the `enable_weak_ref` compat flag), so toolchains such as Emscripten and wasm-bindgen can automatically free WebAssembly heap allocations.

While this will reduce long-running memory growth and leaks to a certain extent, the API still depends on garbage-collector timing and other non-deterministic factors, so treat it as a helpful mitigation rather than a guaranteed fix.
24 changes: 24 additions & 0 deletions src/content/compatibility-flags/js-weak-refs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
_build:
publishResources: false
render: never
list: never

name: "Enable `FinalizationRegistry` and `WeakRef`"
sort_date: "2025-05-05"
enable_date: "2025-05-05"
enable_flag: "enable_weak_ref"
disable_flag: "disable_weak_ref"
---

Enables the use of [`FinalizationRegistry`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) and [`WeakRef`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef) built-ins.
* `FinalizationRegistry` allows you to register a cleanup callback that runs after an object has been garbage-collected.
* `WeakRef` creates a weak reference to an object, allowing it to be garbage-collected if no other strong references exist.

:::note[Behaviour]
`FinalizationRegistry` cleanup callbacks may execute at any point during your request lifecycle, even after your invoked handler has completed (similar to `ctx.waitUntil()`). These callbacks do not have an associated async context — you cannot perform any I/O within them, including emitting events to a tail Worker.
:::

:::caution
These APIs are fundamentally non-deterministic — the timing and execution of garbage collection are unpredictable, and you **should not rely on them for essential program logic**. Additionally, cleanup callbacks registered with `FinalizationRegistry` may **never be executed**, including but not limited to cases where garbage collection is not triggered, or your Worker gets evicted.
:::