docs(observability): record why randomUUID is the fast option, not the lazy one

Benchmarked the alternatives rather than leaving the question open. Every
supported runtime already batches entropy behind randomUUID, so the intuitive
replacements lose: Math.random() twice into base36 is 3.5x slower at 265ns
because the string work dominates, an unbatched getRandomValues is 472ns, and
a uint32 pool through toString(36) is 114ns.

The only cross-runtime improvement is a hand-rolled entropy pool with a hex
lookup table at 50ns, which saves 26ns — 0.26% of a real 10.19us request. Not
worth owning an entropy pool for. Buffer.toString('hex') matches it but is
Node-only, which this file cannot be.

Table left in the code so the next person does not re-run it.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 14:45:17 -05:00
1 parent dabab1812b
commit 7b0a3d4e24
1 file changed
+13 -7
+13 -7
View File
@@ -51,13 +51,19 @@ export function correlateRequest(headers: Record<string, string | string[] | und
const incoming = first(headers['x-request-id'])?.trim();
const traceId = first(headers.traceparent)?.trim();
// Eager, and `randomUUID` rather than something cheaper, both deliberately. A lazy getter is
// defeated the moment anything spreads this object — which the adapters and the payload builders
// both do — so it would buy nothing without threading a nested correlation object through the
// whole request path. And a boot-prefix counter measured 5x cheaper (14.7ns vs 77.1ns) but
// trades the standard identifier shape for ~1.7% of a real request. Neither is worth its
// complexity until a profile says otherwise; the expensive part was building payloads for
// nobody, and `Bus.hasListeners` is what fixed that.
// `randomUUID` is not the lazy choice here, it is the measured one. Every supported runtime
// already batches entropy behind it, so the obvious "faster" replacements are not:
//
// crypto.randomUUID() 76 ns
// batched getRandomValues + hex, 12 bytes 50 ns cross-runtime, saves 0.26% of a request
// batched + Buffer.toString('hex') 49 ns Node-only, so it cannot be used here
// uint32 -> toString(36) from a pool 114 ns
// Math.random() x2 -> base36 265 ns 3.5x SLOWER; string work dominates
// getRandomValues per call, unbatched 472 ns
//
// The only cross-runtime win is 26 ns, against ~10.19 us for a real socketed request. A hand-
// rolled entropy pool is not worth 0.26%. A lazy getter is separately useless: the adapters and
// the payload builders both spread this object, and the first spread materialises it.
return { requestId: incoming || crypto.randomUUID(), traceId: traceId || undefined };
}