measure-fn · The Plain Web Stack · diadem.fun
The Plain Web Stack5 / 15

measure-fn

Source: https://github.com/7flash/measure-fn · bun add measure-fn

import { createMeasure } from "measure-fn";
const api = createMeasure("api");
await api.root("GET /users", async () => {
  const users = await api("load users", () => db.users.select().all());
  return api("render", () => renderUsers(users));
});
[api] a → GET /users
[api] a-a → load users
[api] a-a ✓ 1.20ms → [2 items]
[api] a-b → render
[api] a-b ✓ 0.31ms → "<ul>…"
[api] a ✓ 1.74ms

Each line has a scope, a path showing nesting (a-b is the second child of a), a label, and on completion a status, a duration and a short result. The wrapped function returns exactly what it returned before. An error is logged and then rethrown unchanged.

Need Use
synchronous code api.sync("label", () => ...)
retries api.retry("label", { attempts: 3, retryIf }, fn)
a fallback value instead of a throw { catch: () => fallback }
only failures and slow steps configure({ level: "errors", slowThreshold: 50 })
nothing at all (tests) configure({ silent: true })
Nesting survives await on Node.js and Bun through AsyncLocalStorage. URLs in logs have their secrets redacted.
Next: chapter 6, how all three are tested.