Memoize promise-returning and async functions with configurable caching strategies for performance optimization.
p-memoize is a JavaScript library that memoizes promise-returning and async functions, caching their results to avoid redundant computations and improve performance. It solves the problem of repeated expensive async operations by storing outputs based on input arguments, with built-in support for custom cache strategies and error handling.
JavaScript and Node.js developers working with async functions, APIs, or data-fetching logic who need to optimize performance by caching results.
Developers choose p-memoize for its async-specific design, which avoids caching rejected promises and supports asynchronous cache stores, along with flexible configuration for cache keys, storage, and expiration.
Memoize promise-returning & async functions
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Only caches resolved promises, not rejections, preventing cached errors from affecting subsequent calls, which is explicitly mentioned in the README as a key enhancement over general memoization libraries.
Supports custom key generation, such as using JSON.stringify for objects, allowing fine-grained control over cache hits and misses based on function arguments, as detailed in the caching strategy section.
Works with various cache stores like Map, WeakMap, LRU caches (e.g., quick-lru), or external databases, enabling integration with existing systems or performance optimizations, as shown in the API options.
Includes a shouldCache function to skip caching based on values or context, such as only caching non-empty arrays, providing granular control over cache writes without affecting reads.
Only supports modern ECMAScript decorators (TypeScript 5.0+), explicitly excluding legacy Babel or TypeScript experimentalDecorators, which can hinder adoption in older codebases.
By default, only the first argument is considered via strict equality, which may lead to cache collisions for multi-argument functions or object arguments, requiring additional setup like custom cacheKey functions.
Time-based expiration relies on third-party libraries like expiry-map, adding complexity and dependencies for what should be a core feature in a caching utility, as noted in the tips section.