performance.now()
- Input
- performance.now()
- Expected output
- milissegundos com casas decimais, desde a abertura da página
The value only ever grows, call after call, even if the operating system's clock gets adjusted in the middle of a measurement.
stopwatch performance api
This stopwatch does not read the system clock to measure duration: it uses performance.now(), a browser API built specifically to measure time intervals with a guarantee Date.now() cannot offer, that the reading never moves backward.
The value only ever grows, call after call, even if the operating system's clock gets adjusted in the middle of a measurement.
Good for knowing the current time, not for measuring a duration: a clock adjustment mid-count can make the result come out negative.
The stopwatch only shows the millisecond digit if the measured resolution is 1ms or better; otherwise it falls back to centiseconds or whole seconds.
The duration stays correct because it is always recomputed from the difference between two readings of the monotonic clock, not by adding up ticks. What may slow down is the on-screen drawing, since browsers throttle frame scheduling in hidden tabs. When you return to the tab, the displayed number jumps straight to the right value.
It is not wrong for knowing the current time, but it is risky for measuring duration: the system clock can be adjusted by network synchronization or by the user during the measurement, and if it moves backward, the calculated difference between start and end can come out negative. performance.now() avoids that problem because it is monotonic by definition.
Because since 2018 browsers deliberately reduce the resolution of performance.now() as a defense against side-channel attacks (the Spectre family), and how much each browser reduces it is not the same everywhere; that is why the stopwatch measures the real resolution on the spot instead of assuming a fixed precision.
Because it is tied to the screen repaint, typically around 60 times per second, and it fires whenever the browser decides to repaint, not at guaranteed time intervals; using that as a time base accumulates error every time a frame is dropped or delayed.
It measures the smallest detectable difference between consecutive clock readings: if that resolution is 1 millisecond or better, it shows milliseconds; up to 10 milliseconds, it shows centiseconds; above that, it falls back to whole seconds, so it never displays a precision the browser cannot actually sustain.
Times, laps and sessions stay in this browser's local storage only and are never sent to a server.