Registry / development / memoize-cache-decorator

memoize-cache-decorator

JSON →
library2.1.0jsnpmunverified

TypeScript decorator for memoizing class methods and getters with cache expiration and clearing support. Version 2.1.0, actively maintained. Supports Node.js and browsers. Differentiators: built-in TTL (time-to-live), two-level cache clearing (method-level and instance-level), custom resolver function, and TypeScript-first design. Lower overhead than libraries like lodash.memoize because it uses decorator syntax and integrates with class-based architecture. Can memoize async, static, and getter functions. Requires TypeScript >4.x and experimentalDecorators.

npm install memoize-cache-decorator
INSTALL
IMPORT
SIG · MEMOIZE-CACHE-DECO
M
memoize-cache-decorator
developmentjavascriptv2.1.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.

memoize
import { memoize } from 'memoize-cache-decorator'
import memoize from 'memoize-cache-decorator'
The package exports a named export 'memoize'. Default import is incorrect. Also, must be used as a decorator on class methods or getters.
clearFunction
import { clearFunction } from 'memoize-cache-decorator'
import { clear } from 'memoize-cache-decorator'
The function to clear cache for a specific memoized function is named 'clearFunction', not 'clear'.
clearInstance
import { clearInstance } from 'memoize-cache-decorator'
Used to clear all memoized caches for an instance of a class.

Demonstrates basic memoization with TTL, then clearing cache for a specific function and for the entire instance.

import { memoize, clearFunction, clearInstance } from 'memoize-cache-decorator'; class Example { @memoize({ ttl: 5000 }) expensiveCalculation(n: number): number { console.log('Calculating...'); return n * 2; } } const example = new Example(); console.log(example.expensiveCalculation(5)); // Calculating... 10 console.log(example.expensiveCalculation(5)); // 10 (cached) // Clear cache for this method on this instance clearFunction(example.expensiveCalculation); console.log(example.expensiveCalculation(5)); // Calculating... 10 // Or clear all memoized caches on the instance clearInstance(example); console.log(example.expensiveCalculation(5)); // Calculating... 10
Debug
Known issues
gotchaDecorators require experimentalDecorators: true in tsconfig or use of TypeScript 5.x with decorators stage 3 support (not compatible).
fix
Add "experimentalDecorators": true to tsconfig.json, or consider using a different library if you require native ECMAScript decorators.
affects: >=1.0.0
deprecatedSince v2.0.0, the package moved from 'memoize-decorator' to 'memoize-cache-decorator'. Old package is deprecated and may have breaking differences.
fix
Update package name from 'memoize-decorator' to 'memoize-cache-decorator' in package.json and imports.
affects: >=2.0.0
gotchaThe memoize decorator can only be used on class methods or getters, not on functions or arrow functions.
fix
Wrap the function in a class method or use a different memoization approach for standalone functions.
affects: >=1.0.0
gotchaThe cache is per-instance. Different instances of the same class have separate caches.
fix
If you need a shared cache across instances, consider using a static method or a different caching strategy.
affects: >=1.0.0
gotchaWhen using with async methods, the returned promise is cached, not the resolved value. Calling the method multiple times before the first resolves may cause multiple executions.
fix
Ensure your async methods are called after the first promise resolves, or implement a queuing mechanism.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Class extends value undefined is not a constructor or null
Trying to use the decorator on a non-class function or in a wrong environment (e.g., plain JS without transpilation).
fix
Ensure you are using the decorator inside a TypeScript class (or with proper Babel decorator transform) and that the import is correct.
Cannot find module 'memoize-cache-decorator'
Package not installed or imported incorrectly in Deno (should use jsr import).
fix
For Node: npm install memoize-cache-decorator. For Deno: import { memoize } from 'jsr:@edwinm/memoize-decorator@2'
Property 'memoize' does not exist on type 'typeof import("memoize-cache-decorator")'
Attempting default import instead of named import.
fix
Use: import { memoize } from 'memoize-cache-decorator'
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
36 hits · last 30 days
node
32
Resources