@knownasrazi/memoize
v1.0.0
Published
Tiny memoization with TTL, max size, custom keyers and LRU eviction.
Maintainers
Readme
memoize
Tiny memoization with TTL, LRU and custom keyers.
Zero dependencies - ~2 KB gzipped - TypeScript - Node + Bun + browser
Install
bun add @knownasrazi/memoize
npm install @knownasrazi/memoizeUsage
import { memoize } from "@knownasrazi/memoize";
const memo = memoize((n: number) => n * 2);
memo(2); // 4 (computed)
memo(2); // 4 (cached)
memoize(slowFn, { ttl: 200 });
memoize(slowFn, { maxSize: 100 });
memoize((a, b) => a + b, { key: ([a, b]) => `${a}-${b}` });
memo.clear(); memo.size(); [...memo.keys()];API
memoize(fn, options?): typeof fn & { clear, keys, size }
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| maxSize | number | 1000 | LRU cap |
| ttl | number | Infinity | Per-entry ms |
| key | (args) => string | JSON.stringify | Cache key |
| onExpire | (key) => void | - | Expiry hook |
Development
git clone https://github.com/knownasrazi/memoize.git
cd memoize
bun install
bun test
bun run build
bun run lintLicense
memoize - remember just enough.
