This specification defines an API that provides the current time in sub-millisecond resolution and such that it is not subject to system clock skew or adjustments.

High Resolution Time Level 2 replaces the first version of High Resolution Time [[HR-TIME]] and includes:

Introduction

The ECMAScript Language specification [[ECMA-262]] defines the Date object as a time value representing time in milliseconds since 01 January, 1970 UTC. For most purposes, this definition of time is sufficient as these values represent time to millisecond precision for any instant that is within approximately 285,616 years from 01 January, 1970 UTC. The DOMTimeStamp is defined similarly [[WebIDL]].

In practice, these definitions of time are subject to both clock skew and adjustment of the system clock. The value of time may not always be monotonically increasing and subsequent values may either decrease or remain the same.

For example, the following script may log a positive number, negative number, or zero.

var mark_start = Date.now();
runSomeApplicationTask();

if (window.console)
  window.console.log('Duration of task: ' + (Date.now() - mark_start));

For certain tasks this definition of time may not be sufficient as it does not allow for sub-millisecond resolution and is subject to system clock skew. For example,

This specification does not propose changing the behavior of Date.now() [[ECMA-262]] as it is genuinely useful in determining the current value of the calendar time and has a long history of usage. The DOMHighResTimeStamp type and the Performance.now method of the Performance interface resolve the issues summarized in this section by providing a monotonically increasing time value in sub-millisecond resolution.

Example

A developer can record and report a high-resolution timeline of their application for further offline analysis.

var task_start = performance.now();
runSomeApplicationTask();
var task_end = performance.now();

// developer provided method to upload runtime performance data
reportEventToAnalytics({
  'task': 'Some document task',
  'start_time': task_start,
  'duration': task_end - task_start
});

Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements are to be interpreted as requirements on user agents.

The IDL fragments in this specification must be interpreted as required for conforming IDL fragments, as described in the Web IDL specification. [[!WebIDL]]

Time Origin

The time origin is the time value from which time is measured:

The DOMHighResTimeStamp Type

The DOMHighResTimeStamp type is used to store a time value measured relative from the time origin or a time value that represents a duration between two DOMHighResTimeStamps.

typedef double DOMHighResTimeStamp;

A DOMHighResTimeStamp SHOULD represent a time in milliseconds accurate to 5 microseconds - see .

If the User Agent is unable to provide a time value accurate to 5 microseconds due to hardware or software constraints, the User Agent can represent a DOMHighResTimeStamp as a time in milliseconds accurate to a millisecond.

The Performance interface

[Exposed=(Window,Worker)]
interface Performance : EventTarget {
    DOMHighResTimeStamp now ();
    serializer = {attribute};
};

The now() method MUST return a DOMHighResTimeStamp representing the time in milliseconds from the time origin to the occurrence of the call to the Performance.now method.

The performance attribute

The GlobalPerformance.performance attribute allows access to performance related attributes and methods from the global object.

[NoInterfaceObject, Exposed=(Window,Worker)]
interface GlobalPerformance {
    [Replaceable]
    readonly    attribute Performance performance;
};

Window implements GlobalPerformance;
WorkerGlobalScope implements GlobalPerformance;

Monotonic Clock

The time values returned when calling the Performance.now method on Performance objects with the same time origin MUST be monotonically increasing and not subject to system clock adjustments or system clock skew. The difference between any two chronologically recorded time values returned from the Performance.now method MUST never be negative if the two time values have the same time origin.

Privacy and Security

Access to accurate timing information, both for measurement and scheduling purposes, is a common requirement for many applications. For example, coordinating animations, sound, and other activity on the page requires access to high-resolution time to provide a good user experience. Similarly, measurement enables developers to track the performance of critical code components, detect regressions, and so on.

However, access to the same accurate timing information can sometimes be also used for malicious purposes by an attacker to guess and infer data that they can't see or access otherwise. For example, cache attacks and statistical fingerprinting is a privacy and security concern where a malicious web site may use high resolution timing data of various browser or application-initiated operations to differentiate between subset of users, and in some cases identify a particular user - see [[CACHE-ATTACKS]].

This specification defines an API that provides sub-millisecond time resolution, which is more accurate than the previously available millisecond resolution exposed by DOMTimeStamp. However, even without this new API an attacker may be able to obtain high-resolution estimates through repeat execution and statistical analysis. To ensure that the new API does not significantly improve the accuracy or speed of such attacks, the recommended minimum resolution of the Performance interface should be set to 5 microseconds.

Mitigating such timing side channel attacks entirely is practically not possible: either all operations would have to execute in a time that does not vary based on the value of any confidential information, or, the application would need to be isolated from any time-related primitives (clock, timers, counters, etc). Neither is practical due to the associated complexity for the browser and application developers and the associated negative effects on performance and responsiveness of applications.

Acknowledgments

The editors would like to thank the following people for contributing to this specification: Karen Anderson, Nat Duca, Tony Gentilcore, Arvind Jain, Jason Weber, Boris Zbarsky, Yossef Oren, Vasileios P. Kemerlis, Simha Sethumadhavan, and Angelos D. Keromytis to acknowledge their contributions to this work.