ProvenPin Install

memoizee sync, promise and TTL memoization: working versions (tested 2026-10-16)

Cache sync and promise-returning functions with memoizee, covering argument-keyed memoization, maxAge TTL expiry and clear/delete invalidation with call-count assertions.

passed4 passed · 0 failed2026-10-16 02:18 UTC
Status
passed
Last passed
2026-10-16 02:18 UTC
Tests
4 / 4
Language
TypeScript
Runtime
node v22.23.3
pinned versions3
memoizee@0.4.17@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false
npm test
test environment
node v22.23.3
linux 6.17.0-1022-azure
x64
runner: github-actions 38839686104

The code

Exactly the files that passed. Your agent can pull them with get_recipe("memoizee-promise-ttl") (install), or see them on GitHub.

package.json
{
  "name": "memoizee-promise-ttl",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {
    "memoizee": "0.4.17"
  },
  "devDependencies": {
    "@types/node": "26.6.5",
    "tsx": "4.23.15"
  }
}
src/memo.ts
import memoize from "memoizee";

export type MemoizedFn<F extends (...args: any[]) => any> = F & {
  clear(): void;
  delete(...args: Parameters<F>): boolean;
};

export interface MemoizeOptions {
  maxAge?: number;
}

export function memoizeSync<F extends (...args: any[]) => any>(
  fn: F,
  options: MemoizeOptions = {},
): MemoizedFn<F> {
  return memoize(fn, options) as unknown as MemoizedFn<F>;
}

export function memoizeAsync<F extends (...args: any[]) => any>(
  fn: F,
  options: MemoizeOptions = {},
): MemoizedFn<F> {
  return memoize(fn, { ...options, promise: true }) as unknown as MemoizedFn<F>;
}
src/memoizee.d.ts
declare module "memoizee" {
  export interface Options {
    maxAge?: number;
    promise?: boolean;
    primitive?: boolean;
    preFetch?: number;
    length?: number | false;
  }

  export interface Memoized<F extends (...args: any[]) => any> {
    (...args: Parameters<F>): ReturnType<F>;
    clear(): void;
    delete(...args: Parameters<F>): boolean;
  }

  function memoize<F extends (...args: any[]) => any>(
    fn: F,
    options?: Options,
  ): Memoized<F>;

  export default memoize;
}
test/memo.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { memoizeSync, memoizeAsync } from "../src/memo.ts";

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

test("sync memoization caches per argument set", () => {
  let calls = 0;
  const add = memoizeSync((a: number, b: number) => {
    calls++;
    return a + b;
  });

  assert.equal(add(1, 2), 3);
  assert.equal(add(1, 2), 3);
  assert.equal(add(2, 3), 5);
  assert.equal(calls, 2);
});

test("promise-mode memoizes async calls and shares in-flight promise", async () => {
  let calls = 0;
  const fetchValue = memoizeAsync(async (id: string) => {
    calls++;
    await delay(10);
    return `value:${id}`;
  });

  const [a, b] = await Promise.all([fetchValue("x"), fetchValue("x")]);
  assert.equal(a, "value:x");
  assert.equal(b, "value:x");
  assert.equal(await fetchValue("x"), "value:x");
  assert.equal(calls, 1);
});

test("maxAge expires cached sync entries after TTL", async () => {
  let calls = 0;
  const compute = memoizeSync(
    (n: number) => {
      calls++;
      return n * 2;
    },
    { maxAge: 40 },
  );

  assert.equal(compute(5), 10);
  assert.equal(compute(5), 10);
  assert.equal(calls, 1);

  await delay(60);

  assert.equal(compute(5), 10);
  assert.equal(calls, 2);
});

test("delete removes one key and clear removes all keys", () => {
  let calls = 0;
  const square = memoizeSync((n: number) => {
    calls++;
    return n * n;
  });

  assert.equal(square(2), 4);
  assert.equal(square(3), 9);
  assert.equal(calls, 2);

  square.delete(2);
  assert.equal(square(2), 4);
  assert.equal(calls, 3);

  square.clear();
  assert.equal(square(3), 9);
  assert.equal(calls, 4);
});

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.