memoize
Speed up repeated function calls by caching results for identical input arguments.
Repository Health
Technical Analysis
memoize is Sindre Sorhus’s small utility for caching the result of function calls so repeated invocations with the same input return instantly instead of recomputing. By default it compares only the first argument via strict equality, but it supports pluggable cache stores and custom cache-key functions for multi-argument or by-value comparisons, plus TTL-based expiry (maxAge) so cached memory is released automatically over time.
What You Get
- A single memoize() wrapper function with zero configuration required for the common case
- Configurable cacheKey function for memoizing on multiple arguments or object-by-value equality
- TTL-based expiry via the maxAge option, releasing cached entries automatically once they go stale
- Support for swapping in a custom Map-like cache implementation (e.g. an LRU cache) via the cache option
- Works transparently with Promise-returning/async functions, caching the pending promise itself
Common Use Cases
- Caching expensive pure computations (parsing, formatting, hashing) that get called repeatedly with the same inputs
- Rate-limiting outbound HTTP calls by memoizing a fetch wrapper with a short maxAge TTL
- Avoiding redundant re-renders or recalculations in performance-sensitive application code
- Deduplicating in-flight async calls so concurrent callers share one underlying promise instead of firing duplicate requests
Under The Hood
Architecture memoize is a single-module wrapper (index.ts) that intercepts calls to the target function, computes a cache key (default: the first argument), and short-circuits execution when that key is already present in an internal Map-like store, with special handling to cache in-flight promises for async functions. Tech Stack Written in TypeScript targeting Node.js 22+, published as a pure ESM package with zero runtime dependencies, built with tsc and tested with ava plus tsd for type-level testing, and linted with xo. Code Quality The project enforces xo linting and tsd type-definition tests as part of its test script, ships full TypeScript types, and maintains a readme kept in sync with its sibling package p-memoize for consistent documentation across Sindre Sorhus’s caching utilities. API Design The API is deliberately minimal — a single default export with an options bag — following the same low-ceremony style as the rest of Sindre Sorhus’s utility library ecosystem, making it approachable for anyone who has used sindresorhus packages before.
Used by 2 apps in this directory
Abby
Developer Tools · Product Management
Statically typed feature flags, remote config, and A/B testing with framework-native SDKs for TypeScript teams.
Webstudio
Design Tools · No Code Platforms
Open source visual development platform with complete CSS control, headless CMS integration, and full infrastructure ownership