Timestamps and scheduling guides

Elapsed time measurement: monotonic versus wall clocks

Use a monotonic clock for in-process durations and wall-clock timestamps for cross-system event records.

Nav Tools ·

Separate clock purposes

Date.now reads civil time, which can be adjusted by synchronization or a user. Subtracting two readings can therefore produce misleading runtime measurements.

A monotonic clock is designed to move forward independently of such wall-clock changes.

const start = performance.now();
// await the operation
const elapsedMs = performance.now() - start;

Do not compare unrelated monotonic readings

Monotonic readings are relative to a time origin. Two processes or browser contexts need not share that origin.

For event exchange, record an explicit UTC timestamp and relevant clock uncertainty. Do not send performance.now as if it were Unix time.

Account for runtime details

High-resolution timers can reduce precision for privacy, and sleep behavior has platform nuances. Monotonic does not mean infinitely precise.

Consult the runtime's clock documentation and test suspend/resume behavior if a duration crosses machine sleep.

Use date tools for recorded timestamps

Date Difference compares supplied civil timestamps. It cannot recover whether a process clock jumped while measuring a task.

Store measured duration independently from start and end wall timestamps when both operational latency and audit time matter.

References

← Timestamps and scheduling guides