Optional CreatePerfTimerOptions to configure logging and callbacks.
A PerfTimer with lap() and stop() methods.
const timer = createPerfTimer();
await fetchData();
timer.lap('fetch');
await processData();
timer.lap('process');
const { laps, totalMs } = timer.stop();
// laps: [{ label: 'fetch', durationMs: 42.1, elapsedMs: 42.1 },
// { label: 'process', durationMs: 18.3, elapsedMs: 60.4 }]
// totalMs: 60.4
// Log each lap and get a stop callback
const timer = createPerfTimer({
label: 'pipeline',
log: true,
onLap: (lap) => metrics.record(lap.label, lap.durationMs),
onStop: (result) => metrics.record('total', result.totalMs),
});
await fetchData();
timer.lap('fetch'); // logs: pipeline | fetch | 42.10ms (elapsed: 42.10ms)
await processData();
timer.lap('process'); // logs: pipeline | process | 18.30ms (elapsed: 60.40ms)
timer.stop(); // logs: pipeline | stop | total: 60.41ms | 2 laps
Creates a performance timer for measuring labelled segments of code.
Call
lap(label)after each step to record its duration relative to the previous lap (or the start). Callstop()to finalise and retrieve all laps with their durations.Useful for profiling multi-step pipelines where
benchmarkis too coarse — it gives you per-segment timings without requiring separateperformance.now()calls everywhere.