Skip to main content
Home

Built and signed on GitHub Actions

Memoize any function

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
5 months ago (0.2.1)

memz JSR

Wrap any function with a cache.

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:

// Start with 2 and 1, i.e. create the Lucas sequence.
const cache = { "[0]": 2, "[1]": 1 };
const fib = memoize(
    (n: number): number => fib(n - 2) + fib(n - 1),
    { cache },
);

By default, cache keys are arguments serialized to JSON, hence the "[0]" and "[1]" keys above.

Custom cache keys:

const fib = memoize(
    (n: number): number => fib(n - 2) + fib(n - 1),
    { keyFn: n => n },
);
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@korkje/memz

Import symbol

import * as memz from "@korkje/memz";
or

Import directly with a jsr specifier

import * as memz from "jsr:@korkje/memz";

Add Package

pnpm i jsr:@korkje/memz
or (using pnpm 10.8 or older)
pnpm dlx jsr add @korkje/memz

Import symbol

import * as memz from "@korkje/memz";

Add Package

yarn add jsr:@korkje/memz
or (using Yarn 4.8 or older)
yarn dlx jsr add @korkje/memz

Import symbol

import * as memz from "@korkje/memz";

Add Package

vlt install jsr:@korkje/memz

Import symbol

import * as memz from "@korkje/memz";

Add Package

npx jsr add @korkje/memz

Import symbol

import * as memz from "@korkje/memz";

Add Package

bunx jsr add @korkje/memz

Import symbol

import * as memz from "@korkje/memz";