performance.now vs Date.now in JavaScript
In JavaScript, Date.now() and
performance.now() are both used to measure time, but they
serve entirely different purposes. While Date.now() returns
the current wall-clock time relative to the Unix epoch,
performance.now() provides high-resolution, monotonic
timestamps measured from the start of the document’s lifecycle.
Understanding their differences in precision, time origin, and
reliability is essential for choosing the right tool for benchmarking,
animations, or logging.
1. Precision and Output Format
Date.now(): Returns an integer representing the time in milliseconds. Its precision is limited to whole milliseconds (e.g.,1710000000000).performance.now(): Returns a floating-point number representing milliseconds with microsecond resolution (fractions of a millisecond, e.g.,123.456789). Note that modern browsers slightly reduce this precision to protect against timing attacks like Spectre.
2. Time Origin (Baseline)
Date.now(): Measures time elapsed since the Unix Epoch: January 1, 1970, 00:00:00 UTC. It reflects the real-world calendar date and time.performance.now(): Measures time elapsed since the document was created (defined byperformance.timeOrigin), such as when the web page began loading or the Node.js process started.
3. Monotonicity and System Clock Reliance
Date.now()is non-monotonic: It relies directly on the operating system’s clock. If the system clock is manually adjusted, corrected via an NTP (Network Time Protocol) synchronization, or altered by leap seconds, the value returned byDate.now()can jump forward, jump backward, or freeze.performance.now()is monotonic: It uses a monotonic clock that increases at a steady, constant rate. It is completely independent of the system clock and is guaranteed never to decrease or jump erratically due to system time adjustments.
4. Comparison Summary
| Feature | Date.now() |
performance.now() |
|---|---|---|
| Resolution | Milliseconds (Integer) | Microseconds (Floating-point) |
| Reference Point | Unix Epoch (Jan 1, 1970) | Page/Process creation time |
| Clock Type | Wall-clock (Non-monotonic) | Monotonic clock |
| Affected by System Time Adjustments | Yes | No |
When to Use Which
- Use
Date.now()when you need real-world dates and timestamps, such as displaying the current date to a user, storing creation timestamps in a database, or checking expiration times. - Use
performance.now()when measuring elapsed time, benchmarking code execution speed, calculating animation frame deltas, or computing physics engines where precise and consistent intervals are critical.