Sign In

weakref-store

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weakref-store

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

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

weakref-store

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

Install

npm install weakref-store

Usage

import WeakCache from 'weakref-store';

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.

  • WeakRef - MDN WeakRef documentation
  • FinalizationRegistry - MDN FinalizationRegistry documentation

License

MIT

Keywords

cache

FAQs

Package last updated on 16 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts