@mstuart/iterable-ops

A WeakRef-based cache that automatically evicts entries when values are garbage collected


Keywords
cache, weak, weakref, gc, garbage, collection, finalization, registry, evict
License
MIT
Install
npm install @mstuart/iterable-ops@1.0.0

Documentation

weak-cache

A WeakRef-based cache that automatically evicts entries when values are garbage collected

Install

npm install weak-cache

Usage

import WeakCache from 'weak-cache';

const cache = new WeakCache({
	onEvict(key) {
		console.log(`${key} was garbage collected`);
	},
});

let value = {data: 'hello'};
cache.set('key', value);

console.log(cache.get('key'));
//=> {data: 'hello'}

console.log(cache.has('key'));
//=> true

API

new WeakCache(options?)

options

Type: object

onEvict

Type: (key) => void

Callback invoked with the key when an entry is evicted by garbage collection.

.get(key)

Returns the value for the key, or undefined if the key does not exist or its value has been garbage collected.

.set(key, value)

Set a key-value pair. The value must be an object (required for WeakRef). Throws TypeError for primitives.

.has(key)

Returns true if the key exists and the value is still alive.

.delete(key)

Removes an entry. Returns true if the entry existed.

.size

The approximate number of entries. May include entries whose values have been collected but not yet finalized.

Related

License

MIT