Skip to content

korkje/memz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memz JSR

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);