From 7b0a3d4e246f737bd9db2e6dad6257368002790f Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 16 Aug 2026 14:45:17 -0500 Subject: [PATCH] docs(observability): record why randomUUID is the fast option, not the lazy one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/http/core.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/http/core.ts b/src/http/core.ts index 37528f1..cace5ff 100644 --- a/src/http/core.ts +++ b/src/http/core.ts @@ -51,13 +51,19 @@ export function correlateRequest(headers: Record 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 }; }