Skip to content

memo

Creates a memoized function. The returned function will only execute the source function when no value has previously been computed. If a ttl (milliseconds) is given previously computed values will be checked for expiration before being returned.

Usage

const calls: number[] = [];
const fib = memo((x: number) => {
calls.push(x);
return x < 2 ? x : fib(x - 1) + fib(x - 2);
});
fib(10); // 55
fib(10); // 55 (calls === [10])
fib(11); // 89 (calls === [10, 11])

Installation

Terminal window
npx atmx add helper memo