Sync function type
Sync function to memoize
Memoization options (adapter, staleIn, staleTimeout not supported)
Enhanced memoized function with cache management methods
// Basic usage
const fibonacci = (n: number): number => {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
};
const memoFib = memoizeSync(fibonacci);
memoFib(40); // Computed once and cached
memoFib(40); // Instant return from cache
// With custom key and TTL
const parseJSON = (str: string) => JSON.parse(str);
const memoizedParse = memoizeSync(parseJSON, {
generateKey: (str) => str.substring(0, 100), // Cache by first 100 chars
ttl: 300000, // 5 minutes
maxSize: 50
});
// Conditional caching - bypass cache for specific calls
const expensiveCalc = (value: number, opts?: { bustCache?: boolean }) => {
return value * 2 + Math.random();
};
const smartCalc = memoizeSync(expensiveCalc, {
shouldCache: (value, opts) => !opts?.bustCache,
ttl: 60000
});
// This call uses cache
smartCalc(42);
// This call bypasses cache and executes directly
smartCalc(42, { bustCache: true });
Memoizes a synchronous function with intelligent caching and LRU eviction.
What it does:
What it doesn't do:
When to use:
Performance notes: