typescript-memoize is a decorator library for TypeScript that enables method and getter memoization, primarily for optimizing performance by caching the results of expensive operations. As of version 1.1.1, it provides `@Memoize` for standard caching and `@MemoizeExpiring` for time-limited caching, which automatically invalidates cached values after a specified duration. The library offers flexibility in how memoization keys are generated, supporting methods without parameters, `get` accessors, and methods where memoization is based on specific parameters or custom hash functions. It's a stable library, though release cadence is not explicitly defined, new versions appear infrequently. Its key differentiator lies in its decorator-based approach, integrating seamlessly with TypeScript classes, and allowing for granular control over cache invalidation via custom hash functions or expiration.
npm install typescript-memoizeVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic `@Memoize`, `@MemoizeExpiring` with a time limit, and `@Memoize` with a custom hash function for complex parameters, showing cache hits and expirations in a class context.
Ensure `tsconfig.json` includes `"experimentalDecorators": true, "emitDecoratorMetadata": true` under `compilerOptions`.
Provide a custom hash function to `@Memoize(hashFunction)` that generates a unique key based on all relevant parameters. For example: `@Memoize((p1, p2) => `${p1}-${p2}`).Consider using `MemoizeExpiring` if you need time-based invalidation, or implement a custom memoization solution or a wrapper around `typescript-memoize` that exposes cache clearing functionality if dynamic invalidation is crucial.
Add or ensure `"experimentalDecorators": true` and `"emitDecoratorMetadata": true` in the `compilerOptions` section of your `tsconfig.json`.
Use ES Module import syntax: `import { Memoize } from 'typescript-memoize';`No dependency data recorded yet.