Weak references
WeakRef and FinalizationRegistry expose limited hooks for memory-sensitive patterns. They allow code to reference objects without forcing them to stay alive indefinitely.
Overview
WeakRef and FinalizationRegistry expose limited hooks for memory-sensitive patterns. They allow code to reference objects without forcing them to stay alive indefinitely.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 84 | 84 | 79 | 14.1 | 84 | 14.5 | |
| The FinalizationRegistry() constructor creates FinalizationRegistry objects. | 84 | 84 | 79 | 14.1 | 84 | 14.5 |
| The register() method of FinalizationRegistry instances registers a value with this FinalizationRegistry so that if the value is garbage-collected, the registry's callback may get called. | 84 | 84 | 79 | 14.1 | 84 | 14.5 |
FinalizationRegistry.register.symbol as target Non-registered symbol as target | 109 | 109 | 146 | 16.4 | 109 | 16.4 |
| The unregister() method of FinalizationRegistry instances unregisters a target value from this FinalizationRegistry. | 84 | 84 | 79 | 14.1 | 84 | 14.5 |
FinalizationRegistry.unregister.symbol as target Non-registered symbol as target | 109 | 109 | | 16.4 | 109 | 16.4 |
| Other | ||||||
| A WeakRef object lets you hold a weak reference to another object, without preventing that object from getting garbage-collected. | 84 | 84 | 79 | 14.1 | 84 | 14.5 |
| Built-in object | ||||||
| The WeakRef() constructor creates WeakRef objects. | 84 | 84 | 79 | 14.1 | 84 | 14.5 |
WeakRef.WeakRef.symbol as target Non-registered symbol as target | 109 | 109 | 146 | 16.4 | 109 | 16.4 |
| The deref() method of WeakRef instances returns this WeakRef's target value, or undefined if the target value has been garbage-collected. | 84 | 84 | 79 | 14.1 | 84 | 14.5 |
Syntax
let target = { data: 'important' };
const ref = new WeakRef(target);
ref.deref(); // { data: 'important' }
target = null; // Becomes a candidate for garbage collection
// ref.deref(); // undefined (after garbage collection) Live demo
Create and dereference a WeakRef
Wrap an object in WeakRef and inspect the current dereferenced value.
Use WeakRef in a cache
Keep a lightweight reference in a cache without forcing long-term retention.
Compare direct objects and WeakRef
See the difference between the original object and its weak reference wrapper.
Use cases
Opportunistic caches
Keep references to expensive objects without guaranteeing they stay in memory forever.
Resource cleanup hooks
Track cleanup for wrapper objects that mirror underlying resources when deterministic disposal is unavailable.
Cautions
- Garbage collection timing is intentionally non-deterministic, so weak references are not a control-flow tool.
- Most application code should prefer explicit ownership and disposal over GC-dependent logic.
Accessibility
- This feature does not directly change accessibility, but memory-heavy apps can benefit from better stability.
- Never rely on finalization timing for user-visible behavior or essential state updates.
Related links
Powered by web-features