measure-fn: see where the time goes · Gor Berlian · diadem.fun
← Gor Berlian
ArticleNov 18, 20262 min read

measure-fn: see where the time goes

Contents

Repository: https://github.com/7flash/measure-fn · Package: bun add measure-fn or npm install measure-fn (version 6.0.0) · License: MIT · Dependencies: none

measure-fn wraps a named operation, times it and prints one compact line when it starts and one when it ends. It tracks nesting, so a request shows its database calls underneath it. The wrapped function returns its normal result, and errors are logged and then rethrown unchanged.

import { createMeasure } from "measure-fn";

const api = createMeasure("api");

const count = await api.root("GET /users", async () => {
  const users = await api("load users", async () => [{ id: 1 }, { id: 2 }]);
  return await api("count users", async () => users.length);
});
[api] a → GET /users
[api] a-a → load users
[api] a-a ✓ 0.40ms → [{"id":1},{"id":2}]
[api] a-b → count users
[api] a-b ✓ 0.08ms → 2
[api] a ✓ 0.95ms → 2

What you get#

  • Nesting that survives await. Node.js and Bun use AsyncLocalStorage, so a step inside a request stays a child of that request.
  • Sync and async versions: api.sync(...) for synchronous code, and wrap() for functions, including generators.
  • Retries, timeouts and fallbacks. .retry() honours retryIf, and catch gives a fallback value.
  • Quiet or filtered. Silent mode, levels such as errors (only failures and slow calls) and per-scope filters.
  • Safe logs. Secrets in logged URLs are redacted.
  • Works in browsers, with a separate build.

Where it runs#

measure-fn is the logging layer of the other two webstack libraries:

Proof it works#

  • A 418-case behaviour matrix covers every API path, in sync and async, success and error, with nesting, retries, timeouts, silent and filtered modes. It found one real bug: turning on silent mode during a span dropped that span's end line. The fix is on the next version's branch.
  • 524 tests pass under Bun.
  • Next: a benchmark of the per-call cost when logging is on, off or filtered, and a real-run check under HTTP load. The load check confirms that every started span gets its end line, that ids stay unique and that nesting stays intact. The next version (6.1) is released only after that benchmark shows a clean result on a quiet machine.
  • Source and README (with an upgrade guide from 3.x, 4.x and 5.x): https://github.com/7flash/measure-fn
  • Report on the webstack real-run checks: https://diadem.fun/coin/berlian/webstack-real-run-report
Gor Berlian

One person, many companies, one coin. Every project I build sends a share of its revenue back to buy $BERLIAN on the open market, and every buy is published with its transaction.

See the page

More from Gor Berlian