← run

js-10-memoize-async

0.375
3/8 tests· concurrency
Challenge · difficulty 5/5
# Async memoize with TTL and in-flight dedup

Implement an ES module **`solution.js`** (no external libraries):

```js
export function memoizeAsync(fn, { ttlMs, now = Date.now } = {}) { /* ... */ }
```

Return a memoized version of the async function `fn`. The cache key is
`JSON.stringify(args)` (the array of arguments the wrapper was called with).

Behavior:
- **Cache hit:** if a previous call with the same key resolved within the last `ttlMs`
  milliseconds, return the cached value **without calling `fn` again**.
- **In-flight dedup:** if a call with the same key is already pending (its promise has not
  settled yet), a new call with that key must return the **same in-flight promise** — `fn`
  is invoked only once for concurrent identical calls.
- **Expiry:** once a cached entry is older than `ttlMs`, the next call with that key calls
  `fn` again and refreshes the entry.
- Different keys are cached independently.

**Injectable clock:** time is read via the `now` option (a function returning the current
time in ms), which defaults to `Date.now`. Tests pass a controllable `now` so expiry is
deterministic. Timestamp a cache entry using `now()` when it resolves (or when the call
starts — either is acceptable as long as expiry is measured against `now()`).

If a pending call rejects, the entry must not be cached (the next call retries).

Example:
```js
let calls = 0;
let t = 1000;
const slow = async (x) => { calls++; return x * 2; };
const m = memoizeAsync(slow, { ttlMs: 100, now: () => t });

await Promise.all([m(5), m(5)]); // calls === 1 (deduped)
await m(5);                      // calls === 1 (cache hit)
t += 200;                        // advance past ttl
await m(5);                      // calls === 2 (expired)
```
tests/solution.test.js
import { test } from "node:test";
import { strict as assert } from "node:assert";
import { memoizeAsync } from "./solution.js";

const tick = () => new Promise((res) => setTimeout(res, 1));

test("concurrent identical calls share one in-flight promise (dedup)", async () => {
  let calls = 0;
  const fn = async (x) => {
    calls++;
    await tick();
    return x * 2;
  };
  const m = memoizeAsync(fn, { ttlMs: 1000, now: () => 0 });
  const [a, b, c] = await Promise.all([m(5), m(5), m(5)]);
  assert.equal(a, 10);
  assert.equal(b, 10);
  assert.equal(c, 10);
  assert.equal(calls, 1);
});

test("cache hit within ttl does not call fn again", async () => {
  let calls = 0;
  let t = 1000;
  const fn = async (x) => {
    calls++;
    return x + 1;
  };
  const m = memoizeAsync(fn, { ttlMs: 100, now: () => t });
  assert.equal(await m(7), 8);
  t = 1050; // still within ttl
  assert.equal(await m(7), 8);
  assert.equal(calls, 1);
});

test("entry expires after ttl, fn is called again", async () => {
  let calls = 0;
  let t = 1000;
  const fn = async (x) => {
    calls++;
    return x;
  };
  const m = memoizeAsync(fn, { ttlMs: 100, now: () => t });
  await m("k");
  assert.equal(calls, 1);
  t = 1200; // past ttl
  await m("k");
  assert.equal(calls, 2);
});

test("different keys are cached independently", async () => {
  let calls = 0;
  const fn = async (x) => {
    calls++;
    return x * 10;
  };
  const m = memoizeAsync(fn, { ttlMs: 1000, now: () => 0 });
  assert.equal(await m(1), 10);
  assert.equal(await m(2), 20);
  assert.equal(await m(1), 10); // cached
  assert.equal(calls, 2);
});

test("multiple arguments form the key", async () => {
  let calls = 0;
  const fn = async (a, b) => {
    calls++;
    return a + b;
  };
  const m = memoizeAsync(fn, { ttlMs: 1000, now: () => 0 });
  assert.equal(await m(1, 2), 3);
  assert.equal(await m(1, 2), 3); // hit
  assert.equal(await m(2, 1), 3); // different key
  assert.equal(calls, 2);
});

test("rejections are not cached; next call retries", async () => {
  let calls = 0;
  const fn = async () => {
    calls++;
    throw new Error(`boom ${calls}`);
  };
  const m = memoizeAsync(fn, { ttlMs: 1000, now: () => 0 });
  await assert.rejects(() => m("x"), /boom 1/);
  await assert.rejects(() => m("x"), /boom 2/);
  assert.equal(calls, 2);
});

test("defaults to Date.now when no clock provided", async () => {
  let calls = 0;
  const fn = async (x) => {
    calls++;
    return x;
  };
  const m = memoizeAsync(fn, { ttlMs: 10000 });
  await m(42);
  await m(42);
  assert.equal(calls, 1);
});

test("expiry boundary: exactly ttl old is treated as expired", async () => {
  let calls = 0;
  let t = 0;
  const fn = async (x) => {
    calls++;
    return x;
  };
  const m = memoizeAsync(fn, { ttlMs: 100, now: () => t });
  await m("b"); // stored at time 0
  t = 100; // age === ttl -> not < ttl -> expired
  await m("b");
  assert.equal(calls, 2);
});
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```js
export function memoizeAsync(fn, { ttlMs, now = Date.now } = {}) {
  const cache = new Map();
  const inFlight = new Map();

  return async function(...args) {
    const key = JSON.stringify(args);
    const currentTime = now();
    const entry = cache.get(key);

    if (entry && currentTime - entry.timestamp < ttlMs) {
      return entry.value;
    }

    if (inFlight.has(key)) {
      return inFlight.get(key);
    }

    const promise = fn(...args).then(value => {
      cache.delete(key);
      inFlight.delete(key);
      return value;
    });

    inFlight.set(key, promise);

    return promise;
  };
}
```

Test output (stdout)

st_runner/test:1313:7)
    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)
  ...
# Subtest: entry expires after ttl, fn is called again
ok 3 - entry expires after ttl, fn is called again
  ---
  duration_ms: 0.08478
  type: 'test'
  ...
# Subtest: different keys are cached independently
not ok 4 - different keys are cached independently
  ---
  duration_ms: 0.15182
  type: 'test'
  location: '/tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:51:1'
  failureType: 'testCodeFailure'
  error: |-
    Expected values to be strictly equal:
    
    3 !== 2
    
  code: 'ERR_ASSERTION'
  name: 'AssertionError'
  expected: 2
  actual: 3
  operator: 'strictEqual'
  stack: |-
    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:61:10)
    async Test.run (node:internal/test_runner/test:1313:7)
    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)
  ...
# Subtest: multiple arguments form the key
not ok 5 - multiple arguments form the key
  ---
  duration_ms: 0.188231
  type: 'test'
  location: '/tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:64:1'
  failureType: 'testCodeFailure'
  error: |-
    Expected values to be strictly equal:
    
    3 !== 2
    
  code: 'ERR_ASSERTION'
  name: 'AssertionError'
  expected: 2
  actual: 3
  operator: 'strictEqual'
  stack: |-
    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:74:10)
    async Test.run (node:internal/test_runner/test:1313:7)
    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)
  ...
# Subtest: rejections are not cached; next call retries
not ok 6 - rejections are not cached; next call retries
  ---
  duration_ms: 0.352081
  type: 'test'
  location: '/tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:77:1'
  failureType: 'testCodeFailure'
  error: |-
    The input did not match the regular expression /boom 2/. Input:
    
    'Error: boom 1'
    
  code: 'ERR_ASSERTION'
  name: 'AssertionError'
  expected:
  actual:
  error: 'boom 1'
  stack: |-
    fn (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:81:11)
    file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.js:18:21
    file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:84:30
    waitForActual (node:assert:632:21)
    strict.rejects (node:assert:769:31)
    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:84:16)
    Test.runInAsyncScope (node:async_hooks:227:14)
    Test.run (node:internal/test_runner/test:1306:25)
    Test.processPendingSubtests (node:internal/test_runner/test:897:18)
    Test.postRun (node:internal/test_runner/test:1447:19)
  operator: 'rejects'
  stack: |-
    async TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:85:3)
    async Test.run (node:internal/test_runner/test:1313:7)
    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)
  ...
# Subtest: defaults to Date.now when no clock provided
not ok 7 - defaults to Date.now when no clock provided
  ---
  duration_ms: 0.11743
  type: 'test'
  location: '/tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:89:1'
  failureType: 'testCodeFailure'
  error: |-
    Expected values to be strictly equal:
    
    2 !== 1
    
  code: 'ERR_ASSERTION'
  name: 'AssertionError'
  expected: 1
  actual: 2
  operator: 'strictEqual'
  stack: |-
    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:98:10)
    async Test.run (node:internal/test_runner/test:1313:7)
    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)
  ...
# Subtest: expiry boundary: exactly ttl old is treated as expired
ok 8 - expiry boundary: exactly ttl old is treated as expired
  ---
  duration_ms: 0.08602
  type: 'test'
  ...
1..8
# tests 8
# suites 0
# pass 3
# fail 5
# cancelled 0
# skipped 0
# todo 0
# duration_ms 33.442523
Self-repair attempts
[
  {
    "total": 8,
    "answer": "```js\nexport function memoizeAsync(fn, { ttlMs, now = Date.now } = {}) {\n  const cache = new Map();\n  const inFlight = new Map();\n\n  return async function(...args) {\n    const key = JSON.stringify(args);\n    const currentTime = now();\n    const entry = cache.get(key);\n\n    if (entry && currentTime - entry.timestamp < ttlMs) {\n      return entry.value;\n    }\n\n    if (inFlight.has(key)) {\n      return inFlight.get(key);\n    }\n\n    const promise = fn(...args).then(value => {\n      cache.delete(key);\n      inFlight.delete(key);\n      return value;\n    });\n\n    inFlight.set(key, promise);\n\n    return promise;\n  };\n}\n```",
    "passed": 3,
    "test_error": " (node:internal/test_runner/test:1313:7)\n    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)\n  ...\n# Subtest: rejections are not cached; next call retries\nnot ok 6 - rejections are not cached; next call retries\n  ---\n  duration_ms: 0.352081\n  type: 'test'\n  location: '/tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:77:1'\n  failureType: 'testCodeFailure'\n  error: |-\n    The input did not match the regular expression /boom 2/. Input:\n    \n    'Error: boom 1'\n    \n  code: 'ERR_ASSERTION'\n  name: 'AssertionError'\n  expected:\n  actual:\n  error: 'boom 1'\n  stack: |-\n    fn (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:81:11)\n    file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.js:18:21\n    file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:84:30\n    waitForActual (node:assert:632:21)\n    strict.rejects (node:assert:769:31)\n    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:84:16)\n    Test.runInAsyncScope (node:async_hooks:227:14)\n    Test.run (node:internal/test_runner/test:1306:25)\n    Test.processPendingSubtests (node:internal/test_runner/test:897:18)\n    Test.postRun (node:internal/test_runner/test:1447:19)\n  operator: 'rejects'\n  stack: |-\n    async TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:85:3)\n    async Test.run (node:internal/test_runner/test:1313:7)\n    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)\n  ...\n# Subtest: defaults to Date.now when no clock provided\nnot ok 7 - defaults to Date.now when no clock provided\n  ---\n  duration_ms: 0.11743\n  type: 'test'\n  location: '/tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:89:1'\n  failureType: 'testCodeFailure'\n  error: |-\n    Expected values to be strictly equal:\n    \n    2 !== 1\n    \n  code: 'ERR_ASSERTION'\n  name: 'AssertionError'\n  expected: 1\n  actual: 2\n  operator: 'strictEqual'\n  stack: |-\n    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-f1560gmy/solution.test.js:98:10)\n    async Test.run (node:internal/test_runner/test:1313:7)\n    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)\n  ...\n# Subtest: expiry boundary: exactly ttl old is treated as expired\nok 8 - expiry boundary: exactly ttl old is treated as expired\n  ---\n  duration_ms: 0.08602\n  type: 'test'\n  ...\n1..8\n# tests 8\n# suites 0\n# pass 3\n# fail 5\n# cancelled 0\n# skipped 0\n# todo 0\n# duration_ms 33.442523"
  },
  {
    "total": 8,
    "answer": "```js\nexport function memoizeAsync(fn, { ttlMs, now = Date.now } = {}) {\n  const cache = new Map();\n  const inFlight = new Map();\n\n  return async function(...args) {\n    const key = JSON.stringify(args);\n    const currentTime = now();\n    const entry = cache.get(key);\n\n    if (entry && currentTime - entry.timestamp < ttlMs) {\n      return entry.value;\n    }\n\n    if (inFlight.has(key)) {\n      return inFlight.get(key);\n    }\n\n    const promise = fn(...args).then(value => {\n      cache.delete(key);\n      inFlight.delete(key);\n      return value;\n    }).catch(error => {\n      cache.delete(key);\n      inFlight.delete(key);\n      throw error;\n    });\n\n    inFlight.set(key, promise);\n\n    return promise;\n  };\n}\n```",
    "passed": 4,
    "test_error": "...\n# Subtest: different keys are cached independently\nnot ok 4 - different keys are cached independently\n  ---\n  duration_ms: 3.425039\n  type: 'test'\n  location: '/tmp/llmlab-js-10-memoize-async-ffhcvqsz/solution.test.js:51:1'\n  failureType: 'testCodeFailure'\n  error: |-\n    Expected values to be strictly equal:\n    \n    3 !== 2\n    \n  code: 'ERR_ASSERTION'\n  name: 'AssertionError'\n  expected: 2\n  actual: 3\n  operator: 'strictEqual'\n  stack: |-\n    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-ffhcvqsz/solution.test.js:61:10)\n    async Test.run (node:internal/test_runner/test:1313:7)\n    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)\n  ...\n# Subtest: multiple arguments form the key\nnot ok 5 - multiple arguments form the key\n  ---\n  duration_ms: 0.297221\n  type: 'test'\n  location: '/tmp/llmlab-js-10-memoize-async-ffhcvqsz/solution.test.js:64:1'\n  failureType: 'testCodeFailure'\n  error: |-\n    Expected values to be strictly equal:\n    \n    3 !== 2\n    \n  code: 'ERR_ASSERTION'\n  name: 'AssertionError'\n  expected: 2\n  actual: 3\n  operator: 'strictEqual'\n  stack: |-\n    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-ffhcvqsz/solution.test.js:74:10)\n    async Test.run (node:internal/test_runner/test:1313:7)\n    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)\n  ...\n# Subtest: rejections are not cached; next call retries\nok 6 - rejections are not cached; next call retries\n  ---\n  duration_ms: 0.28806\n  type: 'test'\n  ...\n# Subtest: defaults to Date.now when no clock provided\nnot ok 7 - defaults to Date.now when no clock provided\n  ---\n  duration_ms: 0.11523\n  type: 'test'\n  location: '/tmp/llmlab-js-10-memoize-async-ffhcvqsz/solution.test.js:89:1'\n  failureType: 'testCodeFailure'\n  error: |-\n    Expected values to be strictly equal:\n    \n    2 !== 1\n    \n  code: 'ERR_ASSERTION'\n  name: 'AssertionError'\n  expected: 1\n  actual: 2\n  operator: 'strictEqual'\n  stack: |-\n    TestContext.<anonymous> (file:///tmp/llmlab-js-10-memoize-async-ffhcvqsz/solution.test.js:98:10)\n    async Test.run (node:internal/test_runner/test:1313:7)\n    async Test.processPendingSubtests (node:internal/test_runner/test:897:7)\n  ...\n# Subtest: expiry boundary: exactly ttl old is treated as expired\nok 8 - expiry boundary: exactly ttl old is treated as expired\n  ---\n  duration_ms: 0.09217\n  type: 'test'\n  ...\n1..8\n# tests 8\n# suites 0\n# pass 4\n# fail 4\n# cancelled 0\n# skipped 0\n# todo 0\n# duration_ms 31.896519"
  },
  {
    "total": 8,
    "answer": "```js\nexport function memoizeAsync(fn, { ttlMs, now = Date.now } = {}) {\n  const cache = new Map();\n  const inFlight = new Map();\n\n  return async function(...args) {\n    const key = JSON.stringify(args);\n    const currentTime = now();\n    const entry = cache.get(key);\n\n    if (entry && currentTime - entry.timestamp < ttlMs) {\n      return entry.value;\n    }\n\n    if (inFlight.has(key)) {\n      return inFlight.get(key);\n    }\n\n    const promise = fn(...args).then(value => {\n      cache.delete(key);\n      inFlight.delete(key);\n      return value;\n    }).catch(error => {\n      cache.delete(key);\n      inFlight.delete(key);\n      throw error;\n    });\n\n    inFlight.set(key, promise);\n\n    return promise;\n  };\n}\n```",
    "passed": 4,
    "test_error": ""
  }
]