fast-memo
A robust, configurable memoization cache for async/sync functions.
A robust, configurable memoization cache for async and sync functions. Boost performance and reduce redundant computations or API calls with a dead-simple, production-ready caching utility.
- Works with both async and sync functions
- Configurable TTL (time-to-live) for cache entries
- Custom cache key generation
- Optional error caching with separate TTL
- Manual cache clearing (all or by key)
- Cache stats and direct cache access
- Handles concurrent async calls (deduplication)
- TypeScript support
npm install fast-memo
const { memoize } = require('fast-memo');
// An expensive or async function
async function fetchUser(id) {
// Simulate API call
return { id, name: `User ${id}` };
}
// Wrap with memoize
const getCachedUser = memoize(fetchUser);
// Usage
await getCachedUser(1); // Calls fetchUser, caches result
await getCachedUser(1); // Returns cached result instantly
async function fetchData(userId, options, filters) {
// ...
}
const getCachedData = memoize(fetchData);
await getCachedData(1, { active: true }, ['role:admin']);
await getCachedData(1, { active: true }, ['role:admin']); // Cached
Wraps a function with memoization logic.
-
fn
: Function to memoize (sync or async) -
options
(optional):-
ttl
(number): Time-to-live for cache entries in ms (default: 5 minutes) -
keyGenerator
(function): Custom function to generate cache key from arguments (default:JSON.stringify(args)
) -
cacheErrors
(boolean): Whether to cache errors (default:false
) -
errorTTL
(number): TTL for cached errors in ms (default: 30 seconds)
-
A memoized function with extra methods:
-
clear(key?: any[] | null)
: Clear the entire cache or a specific key -
stats()
: Returns{ size, keys }
for the cache -
cache
: Direct access to the underlyingMap
const getCachedUser = memoize(fetchUser, {
keyGenerator: (id, options) => `${id}:${options.active}`
});
const getCachedUser = memoize(fetchUser, { ttl: 60000 }); // 1 minute
const getCachedUser = memoize(fetchUser, {
cacheErrors: true,
errorTTL: 5000 // Cache errors for 5 seconds
});
getCachedUser.clear(); // Clear all cache
getCachedUser.clear([1]); // Clear cache for user 1
console.log(getCachedUser.stats()); // { size: X, keys: [...] }
import { memoize } from 'fast-memo';
async function fetchUser(id: number): Promise<User> { /* ... */ }
const getCachedUser = memoize(fetchUser);
- Arguments are serialized (by default with
JSON.stringify
) to create a unique cache key for each call. - If a cached value exists and is not expired, it is returned instantly.
- If not, the original function is called, and the result is cached.
- For async functions, concurrent calls with the same arguments share the same pending promise.
- Errors are not cached by default, but can be cached with a separate TTL if desired.
- Expensive computations
- API/database calls
- Functions with predictable/repeated inputs
ISC