Last active
October 7, 2025 00:13
-
-
Save seanlinsley/bc10378fd311d75cf6b5e80394be813d to your computer and use it in GitHub Desktop.
Iterable WeakSet in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// spec: https://github.com/tc39/proposal-weakrefs | |
// the spec contains an [iterable WeakMap implementation](https://github.com/tc39/proposal-weakrefs#iterable-weakmaps) | |
// NOTE: this WeakSet implementation is incomplete, only does what I needed | |
// In Firefox Nightly, visit about:config and enable javascript.options.experimental.weakrefs | |
class IterableWeakSet extends Set { | |
add(el) { | |
super.add(new WeakRef(el)) | |
} | |
forEach(fn) { | |
super.forEach(ref => { | |
const value = ref.deref() | |
if (value) fn(value) | |
}) | |
} | |
*[Symbol.iterator]() { | |
for (const ref of super.values()) { | |
const value = ref.deref() | |
if (value) yield value | |
} | |
} | |
} |
Indeed, this is neither a Set
nor a WeakSet
as it breaks all fundamental assumptions about them. To implement a class with a different behavior but a similar API, you should use composition, not inheritance.
If you wanted to support an API similar to WeakSet
along with the iterable protocol, you'd need something similar to the IterableWeakMap
found in the proposal mentioned in the gist's comment, but using added values as the internal WeakMap
keys.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While I agree that it might be possible to implement a working weak set using the technique you suggest, this implementation has issues. It's a (quasi-) bag, not a set. There's no attempt to ensure that a single element is added to the set only once.