Skip to content

.tip

· one of the small things

Measure durations with a monotonic clock, not Date.now

Date.now() reads the wall clock, and NTP can step that clock backwards mid-request. Subtract two of those readings and the duration comes out negative, or an hour long, and your latency histogram records it as fact. A monotonic clock only moves forward.

const started = performance.now();
await handle(job);
jobDuration.record(performance.now() - started);

performance.now() is global in Node and in every browser; Python's is time.monotonic(). Keep Date.now() for timestamps you store and log, where the wall clock is the point.

javascriptreliability