Registry / testing / lru-memoizer

lru-memoizer

JSON →
library3.0.0jsnpmunverified

Memoize async (and sync) function results using an LRU cache. Version 3.0.0 wraps lru-cache v7+. Key differentiator: custom hash function for cache keys, disable option for conditional caching, and optional deep-freeze/clone of results. ESM and CJS compatible, ships TypeScript definitions. Useful for caching expensive async operations like HTTP requests or database calls.

npm install lru-memoizer
INSTALL
IMPORT
SIG · LRU-MEMOIZER
L
lru-memoizer
testingjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

memoizer
import memoizer from 'lru-memoizer'
const memoizer = require('lru-memoizer');
Package supports both ESM and CJS. Default import works for both.
memoizer.sync
import memoizer from 'lru-memoizer'; const { sync } = memoizer;
import { memoizer } from 'lru-memoizer';
memoizer is default export. sync is a property on the default export.
MemoizeOptions
import type { MemoizeOptions } from 'lru-memoizer'
none
Type import for TypeScript. No wrong pattern known.

Creates a memoized async function that fetches JSON data, caching results for 5 minutes with a max of 100 entries.

import memoizer from 'lru-memoizer'; import got from 'got'; const memoizedRequest = memoizer({ load: (url, callback) => { got(url).json().then(data => callback(null, data)).catch(callback); }, hash: (url) => url, max: 100, ttl: 1000 * 60 * 5, }); memoizedRequest('https://api.example.com/data', (err, data) => { if (err) console.error(err); else console.log(data); });
Debug
Known issues
breakingv3.0 uses lru-cache v7 which changed constructor options (e.g., 'maxAge' replaced by 'ttl').
fix
Use 'ttl' instead of 'maxAge' when upgrading from v2 to v3.
affects: <3.0.0 to >=3.0.0
gotchaResults are deep-frozen or deep-cloned only if 'freeze' or 'clone' options are enabled; otherwise mutations affect cached value.
fix
Set 'clone: true' if you need to mutate returned objects safely.
affects: >=0.0.0
gotchaThe 'hash' function must return a string synchronously. If it returns undefined, caching may not work as expected.
fix
Ensure your hash function always returns a string (e.g., return String(key)).
affects: >=0.0.0
deprecatedThe 'length' option from lru-cache is deprecated in favor of 'sizeCalculation' in lru-cache v7+.
fix
Use 'sizeCalculation' instead of 'length' in lru-memoizer options (passed through to lru-cache).
affects: >=3.0.0
Errors
Common errors & fixes
Error: The 'hash' function should return a string.
hash function returned non-string or is missing.
fix
Ensure hash returns a string; e.g., hash: (opts) => opts.url + JSON.stringify(opts.qs)
TypeError: callback is not a function
The 'load' function's last parameter must be a callback, but you may have omitted it or called with wrong signature.
fix
Define load with correct signature: load: function(...args, callback) { ... }
Cannot find module 'lru-memoizer'
Package not installed or import path incorrect.
fix
Run 'npm install lru-memoizer' and ensure correct import (default import from 'lru-memoizer').
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
lru-cacherequiredUnderlying LRU cache implementation
Agent activity
8 hits · last 30 days
node
8
Resources
lru-memoizer — npm install lru-memoizer · libregistry