Skip to content

weak-value-map

Minified1009 BMinzipped389 B

A map with weakly-held values.

Example

ts
import { WeakValueMap } from "@monstermann/weak-value-map";

type User = { id: string; name: string };

const user1: User = { id: "user-1", name: "Alice" };
const user2: User = { id: "user-2", name: "Bob" };

// Create a cache that won't prevent garbage collection of user objects
const cache = new WeakValueMap<string, User>();

cache.set("user-1", user1);
cache.set("user-2", user2);

console.log(cache.get("user-1")); // { id: "user-1", name: "Alice" }
console.log(cache.has("user-2")); // true

// When user objects are no longer referenced elsewhere,
// they are garbage collected and automatically removed from the cache

Installation

sh
npm install @monstermann/weak-value-map
sh
pnpm add @monstermann/weak-value-map
sh
yarn add @monstermann/weak-value-map
sh
bun add @monstermann/weak-value-map