Registry / productivity / lru-memorise

lru-memorise

JSON →
library0.3.1jsnpmunverified

A simple memoization library wrapping a fast LRU cache (tiny-lru). Current stable version is 0.3.1. It supports memoizing both synchronous and asynchronous functions, automatic cache key generation from function arguments via JSON.stringify, configurable TTL and max cache size, and full TypeScript typings. Unlike other memoize libraries, it exposes the underlying cache for inspection and supports custom cache key resolvers. Suitable for Node.js and browsers.

npm install lru-memorise
INSTALL
IMPORT
SIG · LRU-MEMORISE
L
lru-memorise
productivityjavascriptv0.3.1
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.

default
import memoize from 'lru-memorise';
const memoize = require('lru-memorise');
Package ships ESM and CommonJS, but default export is the preferred usage.
MemoizeOptions
import type { MemoizeOptions } from 'lru-memorise';
import { MemoizeOptions } from 'lru-memorise';
TypeScript users should import types with 'import type' to avoid runtime issues.
Resolver
import type { Resolver } from 'lru-memorise';
import { Resolver } from 'lru-memorise';
The Resolver type is for custom cache key functions; imported as type.

Demonstrates memoization of a sync function with TTL and max cache size.

import memoize from 'lru-memorise'; const expensiveFunction = (a: number, b: number): number => { console.log('computing...'); return a + b; }; const memoized = memoize(expensiveFunction, { lruOptions: { max: 100, ttl: 60000 } // cache max 100 entries, TTL 60 seconds }); console.log(memoized(1, 2)); // computes, logs 'computing...', outputs 3 console.log(memoized(1, 2)); // returns from cache, no log console.log(memoized(3, 4)); // computes, logs 'computing...', outputs 7
Debug
Known issues
gotchaCache key generation uses JSON.stringify on arguments by default, which can produce unexpected keys for non-primitive arguments like objects with circular references or undefined values.
fix
Provide a custom cacheKeyResolver that safely serializes arguments.
affects: >=0.1.0
gotchaWhen memoizing async functions, the cache stores the Promise itself, not the resolved value. This means if the function throws, the rejected promise is cached and subsequent calls will also reject immediately.
fix
Ensure your async function never rejects, or clear the cache on rejection.
affects: >=0.1.0
deprecatedThe `_cache` property on the memoized function is not formally documented and may change between minor versions.
fix
Use the `cache` option with an external LRU cache to have full control.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: memoize is not a function
Using CommonJS require incorrectly - the default export is not directly available.
fix
Use `const memoize = require('lru-memorise').default;` for CommonJS.
TS2345: Argument of type 'typeof import(...)' is not assignable to parameter of type '(...args: any[]) => any'
Passing a class or non-function to memoize.
fix
Ensure the first argument is a function (async or sync).
Error: options must be an object
Passing a non-object as the second argument to memoize.
fix
Always pass an object (possibly empty) as options: `memoize(fn, {})`.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies
tiny-lrurequiredUnderlying LRU cache implementation; used internally when no cache is provided
Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources