Memoize any function.
import memoize from "jsr:@korkje/memz";
const add = memoize((a: number, b: number) => a + b);
Recursion:
const fib = memoize((n: number): number => {
if (n < 2) {
return n;
}
return fib(n - 2) + fib(n - 1);
});
Initial cache:
const cache = { "0": 0, "1": 1 };
const fib = memoize((n: number): number => fib(n - 2) + fib(n - 1), cache);