The neighbourhood head’s stopwatch is performance.now() and the tent’s wall clock is Date.now(): every heat is timed by both for real, the stopwatch’s…
Why it matters
Timing a duration with Date.now() can go wrong: too coarse for small work, and negative when the system clock is set. Keeping performance.now() as a time is wrong too: its number only means something on that page.
The key idea
The stopwatch for durations, the wall clock for times
Common mistakes
A duration reads negative when timed with Date.now The system clock can be set mid-measurement (network time sync, the user, time zones). Time durations with performance.now(), which is monotonic.
Keeping performance.now() as a time gives nonsense numbers Its number is relative to when the page opened, so it means nothing on another page or session. For “when”, keep Date.now() or performance.timeOrigin + performance.now().
Expecting nanosecond precision from performance.now Its resolution is reduced on purpose (about 0.1 ms usually, 5 µs in a cross-origin isolated page) for security. For very small work, time many runs and take the average.
Try it in this playground
Blow the whistle for one heat The stopwatch reads fractions, the wall clock whole numbers.
Find the stopwatch’s smallest step Not nanoseconds: browsers coarsen it on purpose.
Set the wall clock back mid-heat The duration from Date.now() turns negative.
Keep performance.now(), then open a new page The new page starts again near 0.