# Subverting control with weak references

DevFeed: [Subverting control with weak references](<https://devfeed.tech/articles/subverting-control-with-weak-references-28094.md>)

Original publisher: [Read original article](<https://jlongster.com/subverting-control-weak-refs>)

Author: James Long

Published: 2025-02-24T12:00:00Z

Content type: tutorial

Language: en

Sources: [James Long](<https://devfeed.tech/sources/james-long.md>)

Topics: [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [Memory Leaks](<https://devfeed.tech/topics/memory-leaks.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [data-structure](<https://devfeed.tech/tags/data-structure.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [memory](<https://devfeed.tech/tags/memory.md>), [memory-leaks](<https://devfeed.tech/tags/memory-leaks.md>), [reference](<https://devfeed.tech/tags/reference.md>)

## AI overview

This JavaScript article explains weak references, WeakRef, and WeakMap. It describes how weak references interact with garbage collection and how WeakMap can avoid retaining objects and causing memory leaks.

## Source excerpt

Weak references are neat. The best language features unlock different kinds of abstractions, and weak references do exactly that. Let me show you why. In JavaScript we have two APIs to work with weak references: WeakMap and WeakRef. (Before I wrote this article I thought WeakRef was only a proposal, but it turns out most browsers have already implemented it) One of the more common use cases uses WeakMap. This data structure keeps a weak reference to the keys in the map, and a strong reference between the keys and values. I guess I should explain what a "weak reference" is: usually if you have a reference to an object in a variable, it stops the garbage collector from deleting it (which makes sense). It'd be weird if suddenly your variable pointed to nothing right? A "weak reference" doesn't stop the object from being garbage collected. Usually languages' semantics don't allow a variable to change in the middle of execution however: // this doesn't exist, but what if we could create a weak reference like this? let weak trans = new Transaction(); // do a bunch of things... // error! trans is... nothing? it got garbage collected trans.transfer(); ^3d7637 Wouldn't it be weird if trans changed in the middle of execution? If that was possible all bets would be off for everything in the entire program because these weak references could be passed anywhere. Instead, APIs for weak references force you to call a function to get the value. The WeakRef class has a deref method to get the object. const ref = new WeakRef(obj) // get the object ref.deref() // do a bunch of things... ref.deref() ^ae238d In the above example, if nothing has a reference to obj, it'll eventually get garbage collected. That means deref might return obj the first time, but the second time it might return undefined. Let's get back to WeakMap which is the more common usage of weak references. (Direct weak references have a lot of weird behaviors and they should be considered very low-level.) A WeakMap has