Why Your App Should Use a Monotonic Clock, Not Date.now(), for Measuring Durations
Using Date.now() to measure elapsed time in JavaScript is unreliable because wall clocks can be adjusted forward or backward by time-synchronization software, potentially producing negative or wildly inaccurate durations. A monotonic clock, such as performance.now() in browsers, guarantees that successive readings never decrease, making it the correct tool for timing operations. The danger of wall-clock misuse was demonstrated in a real-world incident: during a leap second on January 1, 2017, Cloudflare's RRDNS service computed negative elapsed times, which caused Go's random number function to panic and disrupted roughly 0.2% of DNS queries across 102 data centers. The root cause was that multiple redundant machines shared the same flawed assumption, so a single correlated external event triggered failures simultaneously. Following this class of incident, Go updated its time model to automatically use monotonic readings when calculating durations, distinguishing between timestamps—used to mark when something happened—and durations, which require a separate, more stable instrument.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in