W3C

High Resolution Time Level 2

W3C First Public and Last Call Working Draft 3 December 2013

This version:
http://www.w3.org/TR/2013/WD-hr-time-2-20131203/
Latest version:
http://www.w3.org/TR/hr-time-2/
Latest Editor's Draft:
http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HighResolutionTime2/Overview.html
Editor:
Jatinder Mann, Microsoft Corp., <>
James Simonsen, Google Inc., <>

Abstract

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

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This is the First Public Working Draft and Last Call Working Draft for the High Resolution Time Level 2 specification.

Please send comments to public-web-perf@w3.org (archived) with [HighResolutionTime2] at the start of the subject line by 8 January 2014.

This document is produced by the Web Performance Working Group. The Web Performance Working Group is part of the Rich Web Clients Activity in the W3C Interaction Domain.

High Resolution Time Level 2 builds on the first version of High Resolution Time and includes:

Publication as a First Public and Last Call Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

  1. 1 Introduction
    1. 1.1 Examples
  2. 2 Conformance requirements
  3. 3 Terminology
  4. 4 High Resolution Time
    1. 4.1 Introduction
    2. 4.2 Time Origin
    3. 4.3 The DOMHighResTimeStamp Type
    4. 4.4 The performance.now() method
    5. 4.5 The workerStart attribute
    6. 4.6 Monotonic Clock
    7. 4.7 Privacy and Security
  5. 5 References
  6. Acknowledgements

1 Introduction

This section is non-normative.

The ECMAScript Language Specification 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.

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();
doTask(); // Some task
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() 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 now method of the Performance interface resolve the issues summarized in this section by providing a monotonically increasing time value in sub-millisecond resolution.

1.1 Examples

This section is non-normative.

A developer may wish to construct a timeline of their entire application, including workers. With a dedicated worker, this is easy. All DOMHighResTimeStamps recorded in the worker use the same time origin as the document.

Worker:
var mark_start = performance.now();
doTaskInWorker(); // Some task
var mark_end = performance.now();
postMessage({'task': 'Some worker task',
             'start_time': mark_start,
             'end_time': mark_end});
	
Document:
var mark_start = performance.now();
doTaskInDocument(); // Some other task
var mark_end = performance.now();
var document_entry = {'task': 'Some document task',
                      'start_time': mark_start,
                      'end_time': mark_end};
var worker = new Worker('js');
worker.onmessage = function (event) {
  var worker_entry = event.data;
  plotTimeline([worker_entry, document_entry]);
}
	

Constructing a timeline with SharedWorkers is slightly more difficult. The time origin is different, because a worker may be shared across multiple documents. In that case, a developer must adjust DOMHighResTimeStamps before displaying them on the same timeline. This can be done with the workerStart attribute.

Document:
var mark_start = performance.now();
doTaskInDocument(); // Some other task
var mark_end = performance.now();
var document_entry = {'task': 'Some document task',
                      'start_time': mark_start,
                      'end_time': mark_end};
var worker = new SharedWorker('js');
worker.port.onmessage = function (event) {
  var worker_entry = event.data;
  worker_entry.start_time -= worker.workerStart;
  worker_entry.end_time -= worker.workerStart;
  plotTimeline([worker_entry, document_entry]);
}
	

Note that the SharedWorkerGlobalScope doesn't have access to its workers' workerStart values. If needed, the document can send a message to the worker containing its workerStart value.

2 Conformance requirements

All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.

The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. For readability, these words do not appear in all uppercase letters in this specification.

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. [Web IDL]

3 Terminology

The construction "a Foo object", where Foo is actually an interface, is sometimes used instead of the more accurate "an object implementing the interface Foo".

The term "JavaScript" is used to refer to ECMA-262, rather than the official term ECMAScript, since the term JavaScript is more widely known.

4 High Resolution Time

4.1 Introduction

This section is non-normative.

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

4.2 Time Origin

The time origin is the time value from which time is measured. The time origin must be equal to the time of the start of navigation of the current document.

Note

The time value of the start of navigation of the document in an attribute of type DOMHighResTimeStamp is equal to 0. The same time value described with an attribute of type DOMTimeStamp is equal to the navigationStart attribute of the PerformanceTiming interface [NavigationTiming].

For a dedicated worker, the time origin must be equal to the time of the start of navigation of the document where it was created.

For a shared worker, the time origin must be equal to the time of creation of the shared worker.

4.3 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.

Type Definition DOMHighResTimeStamp

A DOMHighResTimeStamp SHOULD represent a time in milliseconds accurate to a microsecond.

Note

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


IDL Definition
typedef double DOMHighResTimeStamp;

4.4 The performance.now() method

partial interface Performance {
  DOMHighResTimeStamp now();
};

interface WorkerPerformance {
  DOMHighResTimeStamp now();
};

partial interface WorkerGlobalScope {
  readonly attribute WorkerPerformance performance;
};

now method

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

4.5 The workerStart attribute

partial interface SharedWorker {
  readonly attribute DOMHighResTimeStamp workerStart;
};

workerStart attribute

The workerStart attribute MUST return a DOMHighResTimeStamp representing the difference between the time origin of the SharedWorkerGlobalScope associated with the SharedWorker and the time origin of the current document.

Note

The value returned by workerStart may be negative if a SharedWorkerGlobalScope already existed for the SharedWorker prior to the document's time origin.

4.6 Monotonic Clock

The time values returned when calling the now method 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 now method MUST never be negative.

4.7 Privacy and Security

Statistical fingerprinting is a privacy concern where a malicious web site may determine whether a user has visited a third-party web site by measuring the timing of cache hits and misses of resources in the third-party web site. Though the now method of the Performance interface returns time data to a greater accuracy than before, it does not make this privacy concern significantly worse than it was already.

5 References

5.1 Normative References

[IETF RFC 2119]
Key words for use in RFCs to Indicate Requirement Levels, Scott Bradner, Author. Internet Engineering Task Force, March 1997. Available at http://www.ietf.org/rfc/rfc2119.txt.
[ECMA-262]
ECMAScript Language Specification, 5.1 Edition. ECMA International, Standard ECMA-262, June 2011. This version of the ECMAScript Language is available from http://www.ecma-international.org/publications/standards/Ecma-262.htm.
[High Resolution Time]
High Resolution Time, Jatinder Mann, Editor. World Wide Web Consortium, December 2012. This version of the High Resolution Time Recommendation is available from http://www.w3.org/TR/2012/REC-hr-time-20121217/. The latest version of High Resolution Time is available at http://www.w3.org/TR/hr-time/.
Navigation Timing, Zhiheng Wang, Editor. World Wide Web Consortium, December 2012. This version of the Navigation Timing Recommendation is available from http://www.w3.org/TR/2012/REC-navigation-timing-20121217/. The latest version of Navigation Timing is available at http://www.w3.org/TR/navigation-timing/.
[Web IDL]
Web IDL, Cameron McCormack, Editor. World Wide Web Consortium, April 2012. This version of the Web IDL specification is available from http://www.w3.org/TR/2012/CR-WebIDL-20120419/. The latest version of Web IDL is available at http://www.w3.org/TR/WebIDL/.
[Web Workers]
Web Workers, Ian Hickson, Editor. World Wide Web Consortium, May 2012. This version of the Web Workers specification is available from http://www.w3.org/TR/2012/CR-workers-20120501/. The latest version of Web Workers is available at http://www.w3.org/TR/workers/.

5.2 Informative References

[HTML5]
HTML5, Robin Berjon et al., Editors. World Wide Web Consortium, August 2013. This version of the HTML5 is available from http://www.w3.org/TR/html5/. The latest editor's draft of HTML5 is available at http://www.w3.org/html/wg/drafts/html/CR/.
[Page Visibility]
Page Visibility, Jatinder Mann and Arvind Jain, Editors. World Wide Web Consortium, October 2013. This version of the Page Visibility Recommendation is available from http://www.w3.org/TR/2013/REC-page-visibility-20131029/. The latest version of Page Visibility is available at http://www.w3.org/TR/page-visibility/.

Acknowledgements

I would like to sincerely thank Karen Anderson, Nat Duca, Tony Gentilcore, Arvind Jain, and Jason Weber to acknowledge their contributions to this work.