weakerable
v0.1.0
Published
Iterable WeakMap and WeakSet
Maintainers
Readme
weakerable
Social Media Photo by Pete Nuij on Unsplash
A tiny ESM module that provides iterable WeakMap and WeakSet wrappers.
weakerable keeps the weak ownership semantics of native weak collections while
letting you iterate over keys, values, or entries that are still reachable.
Features
Mapimplements the familiarWeakMapAPI plus[Symbol.iterator]().Setimplements the familiarWeakSetAPI plus[Symbol.iterator]().- Entries are tracked through
WeakRef, so collected keys disappear from future iterations. - Constructors accept iterables, matching
MapandSetergonomics. - TypeScript declarations are included.
Usage
// import Map from "weakerable/map";
// import Set from "weakerable/set";
import { Map, Set } from "weakerable";
// or import * as weak from "weakerable"; new weak.Map([...]);
const key = {};
const cache = new Map([[key, "value"]]);
const seen = new Set([key]);
console.log(cache.get(key)); // "value"
console.log([...cache]); // [[key, "value"]]
console.log([...seen]); // [key]You can also import the individual implementations:
import Map from "weakerable/map";
import Set from "weakerable/set";Reason
I already use not-so-weak on occasion, but I recently realized that all I needed was an iterable WeakSet or WeakMap.
With this module's simpler approach:
- code size stays minimal: iteration is the only extra feature
- performance stays as close as possible to native
WeakMapandWeakSet - there is no
FinalizationRegistryorchestration: stale references are pruned when the collection is iterated
That's it. If you need a minimal iterable weak container, this should be your final stop.
