memoize

Speed up repeated function calls by caching results for identical input arguments.

Library
npm
v11.0.0
1,163stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
50/100Fair
Development Activity36
Maintenance20
Community56
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
70/100Good
Architecture60
Code Quality78
Innovation55
Learning Curve88

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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search