not-so-weak
Version:
Iterable WeakMap, WeakSet and WeakValue.
52 lines (38 loc) • 2.53 kB
Markdown
# Not So Weak
[](https://github.com/WebReflection/not-so-weak/actions) [](https://coveralls.io/github/WebReflection/not-so-weak?branch=main)
<sup>**Social Media Photo by [Pete Nuij](https://unsplash.com/@pete_nuij) on [Unsplash](https://unsplash.com/)**</sup>
Iterable [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) (*WKey*) and [WeakSet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) (*WSet*) through [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) primitives, reimplementing also the [WeakValue](https://github.com/WebReflection/weak-value#readme) (*WValue*) module, including the optional *callback* for collected values.
```js
// const {WSet, WKey, WValue} = require('not-so-weak');
import {WSet, WKey, WValue} from 'not-so-weak';
// class WSet<T extends object> extends WeakSet implements Set {}
// class WKey<K extends object, V> extends WeakMap implements Map {}
// class WValue<K, V extends object> extends Map {}
// node --expose-gc example
const ws = new WSet([{}]);
const wm = new WKey([[{}, 'value']]);
const wv = new WValue;
wv.set('value', {}, function (key) {
console.assert(this === wv);
console.assert(key === 'value');
console.log(key, 'value collected');
});
console.assert(ws.size === 1);
console.assert(wm.size === 1);
console.assert([...wm.values()][0] === 'value');
console.assert([...wv.keys()][0] === 'value');
setTimeout(() => {
gc();
console.assert(ws.size === 0);
console.assert(wm.size === 0);
console.assert(wv.size === 0);
});
```
### Suitable For
* Weak key/value based state/store
* Server Side related tasks that can't bother with manual removal of weakly referenced entries
* every case where you end up swapping to `Map` or `Set` because you realize you cannot iterate over their *Weak* counterpart
* every case where you think there's a memory leak due possibly missing `weakThing.delete(ref)` operations
### Not Suitable For
* raw performance or benchmarks against `Map` or `Set`
* every case where `weakThing.delete(ref)` is already handled by the library or framework logic