Memoization in JS and Python: speed up hot paths

Memoization in JS and Python: speed up hot paths

TL;DR: Cache the results of pure functions to avoid recomputing. Measure before you optimize. Clear caches when inputs or context change.

When memoization helps

  • Pure and expensive computations called repeatedly with the same inputs.
  • Computed selectors and derived data for UI.

Avoid caching for impure functions or when memory is tight.

JavaScript example

function memoize(fn) {
  const cache = new Map();
  return (...args) => {
    const key = JSON.stringify(args);
    if (cache.has(key)) return cache.get(key);
    const val = fn(...args);
    cache.set(key, val);
    return val;
  };
}

const slowFib = n => (n < 2 ? n : slowFib(n - 1) + slowFib(n - 2));
const fastFib = memoize(slowFib);

React notes

Use React.memo for components and useMemo for derived values. Do not memoize everything. Optimize bottlenecks.

Python example

from functools import lru_cache

@lru_cache(maxsize=1024)
def parse_price(s: str) -> int:
    return int(float(s) * 100)

Benchmarks and profiling

  • Use performance.now in JS and timeit in Python.
  • Compare cache hit vs miss performance.

Risks

  • Stale data - purge or key by version.
  • Memory growth - cap size and evict.

FAQ

Is memoization thread safe
JS is single threaded in the main thread. In Python, lru_cache is thread-safe for typical use, but heavy concurrency may require locks.

How do I clear caches
Drop the map or call cache.clear in JS. In Python, use function.cache_clear on lru_cache functions.

Cast this in your project

Image credit: Photo by cottonbro studio: https://www.pexels.com/photo/person-holding-white-iphone-5-c-4705636/

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.