From 9da6fe4c98bdd55f277e157d7034ce7e0372f245 Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Thu, 23 Jul 2026 00:30:32 -0400 Subject: [PATCH] docs: wait-enter --import helper for profiling benchmarks --- examples/wait-enter.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/wait-enter.ts diff --git a/examples/wait-enter.ts b/examples/wait-enter.ts new file mode 100644 index 0000000..3f2ad27 --- /dev/null +++ b/examples/wait-enter.ts @@ -0,0 +1,19 @@ +// Pauses startup until Enter is pressed — handy for attaching a profiler +// or inspector before a benchmark begins. Top-level await in an --import +// module runs to completion before the main module is evaluated. +// +// Usage: node --import ./examples/wait-enter.ts examples/benchmark-peers/main.ts + +import { isMainThread } from 'node:worker_threads'; +import { createInterface } from 'node:readline/promises'; + +// Worker threads inherit execArgv (including --import) — only the main +// thread should prompt; workers must start immediately. +if (isMainThread) { + const rl = createInterface({ input: process.stdin, output: process.stdout }); + await rl.question(`Press Enter to start ${process.argv[1] ?? ''}… `); + rl.close(); + // Don't let stdin pin the event loop afterward — benchmarks and runtime + // examples rely on natural process exit. + process.stdin.unref(); +} -- 2.51.2