source: frontend/node_modules/w3c-hr-time/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 8.9 KB
Line 
1# w3c-hr-time
2
3This module implements the W3C [High Resolution Time Level 2][HR-TIME] specification. It provides exactly three exports:
4
5- `Performance` class
6- `getGlobalMonotonicClockMS(): number`
7- `clockIsAccurate: boolean`
8
9In all APIs, a "high-resolution timestamp" means a number in milliseconds that may have a fractional part, if the system clock is accurate enough (see "Clock accuracy" section below). It is identical to the [`DOMHighResTimeStamp`][] type in the High Resolution Time spec.
10
11Portability is paramount to this module. It uses only APIs exposed from Node.js core like `Date.now()` and `process.hrtime()` and does not use or require any native modules. It also employs the [browser-process-hrtime][] module for graceful degrades for platforms that do not have `process.hrtime()` (such as browsers).
12
13## `Performance` class
14
15```js
16const { Performance } = require("w3c-hr-time");
17const performance = new Performance();
18
19console.log(performance.timeOrigin);
20// Prints a number close to Date.now() but that may have fractional parts, like
21// 1514888563819.351.
22
23console.log(new Date(performance.timeOrigin));
24// Prints a date close to new Date(), like 2018-01-02T10:22:43.819Z.
25
26setTimeout(() => {
27 console.log(performance.now());
28 // Prints a number close to 5000, like 5008.023059.
29}, 5000);
30```
31
32Perhaps the most interesting export is the [`Performance`][] class. By constructing the class, you can get an instance quite similar to the `window.performance` object in browsers. Specifically, the following APIs are implemented:
33
34* [`performance.now(): number`][`Performance#now()`] returns the high-resolution duration since the construction of the `Performance` object.
35* [`performance.timeOrigin: number`][`Performance#timeOrigin`] is a high-resolution timestamp of the `Performance` object's construction, expressed in [Unix time][].
36* [`performance.toJSON(): object`][`Performance#toJSON()`] returns an object with `timeOrigin` property set to the corresponding value of this object. This allows serializing the `Performance` object with `JSON.stringify()`. In browsers, the returned object may contain additional properties such as `navigation` and `timing`. However, those properties are specific to browser navigation and are unsuitable for a Node.js implementation. Furthermore, they are specified not in the High Resolution Time spec but in [Navigation Timing][NAVIGATION-TIMING], and are thereby outside the scope of this module.
37
38### Limitations
39
40This module does not aim for full [Web IDL][WEBIDL] conformance, so things like `performance.toJSON.call({})` will not throw `TypeError`s like it does in browsers. If you need full Web IDL conformance, you may be interested in the [webidl2js][] module.
41
42The `Performance` class provided also does not have `mark()`, `measure()`, `getEntries()`, and such functions. They are specified in other specs than High Resolution Timing, such as [User Timing][USER-TIMING] (marks and measures) and [Performance Timeline][PERFORMANCE-TIMELINE] (entries management). Those specs extend the definition of the `Performance` class defined in High Resolution Timing, and to implement those specs you can extend the `Performance` class exported by this module.
43
44Due to the limitations of the APIs exposed through Node.js, the construction of a `Performance` object may take up to 1 millisecond to gather information for high-resolution `timeOrigin`.
45
46## Global monotonic clock
47
48The High Resolution Time spec defines a [global monotonic clock][] that is "shared by time origin's *[sic]*, is monotonically increasing and not subject to system clock adjustments or system clock skew, and whose reference point is the Unix time."
49
50This module exports a function `getGlobalMonotonicClockMS()` that is the basis of all timing functions used my this module when a monotonic time is required. It returns a high-resolution timestamp whose zero value is at some arbitrary point in the past. (For the current high-resolution timestamp based on the Unix epoch, use `new Performance().timeOrigin` instead.)
51
52```js
53const { getGlobalMonotonicClockMS } = require("w3c-hr-time");
54
55const start = getGlobalMonotonicClockMS();
56console.log(start);
57// Prints a millisecond timestamp based on an arbitrary point in the past, like
58// 280249733.012151.
59
60setTimeout(() => {
61 console.log(getGlobalMonotonicClockMS() - a);
62 // Prints a number close to 5000, like 5006.156536.
63}, 5000);
64```
65
66Unlike other functions that return only integer timestamps if the system clock does not provide enough resolution, this function may still return timestamps with fractional parts on those systems with less accurate clocks. See "Clock accuracy" section below for more information.
67
68## Clock accuracy
69
70The High Resolution Time spec [states][HR-TIME §4] that
71
72> A [`DOMHighResTimeStamp`][] *SHOULD* represent a time in milliseconds accurate to 5 microseconds - see [8. Privacy and Security][HR-TIME §8].
73>
74> 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.
75
76This module implements this suggestion faithfully. It executes a test at `require()`-time to determine if the system clock (both `Date.now()` and `process.hrtime()`) is accurate enough to 5 microseconds. The result of this test can be accessed through the exported `clockIsAccurate` boolean value.
77
78```js
79const { Performance, clockIsAccurate } = require("w3c-hr-time");
80
81const performance = new Performance();
82
83if (!clockIsAccurate) {
84 console.assert(Number.isInteger(performance.timeOrigin));
85 console.assert(Number.isInteger(performance.now()));
86}
87```
88
89If `clockIsAccurate` is false, `performance.timeOrigin` and `performance.now()` are always rounded to millisecond accuracy. `getGlobalMonotonicClockMS()` however is exempt from this requirement due to its best-effort nature, and the fact that it is not an API exposed by the High Resolution Time spec.
90
91## Clock drift
92
93[Clock drift][clock drift] can be observed through system or user clock adjustments -- that is, the speed at which `Date.now()` changes may be faster or slower than real time if there is a pending adjustment to the system clock, for example through NTP synchronizing.
94
95In the spec, the [global monotonic clock][] is defined to be immune to such drifts. Correspondingly, the APIs exposed through this module that are defined using the global monotonic clock such as `performance.now()` and `getGlobalMonotonicClockMS()` are also guaranteed to reflect real time.
96
97For example, if `performance.now()` returns 1000, it is guaranteed that the time of this call is exactly one second since the construction of the `Performance` object. But the difference in `Date.now()`'s value from the construction of the `Performance` object to when `performance.now()` returns 1000 may not be exactly 1000. You may also see `performance.now() - Date.now()` diverge over time as a result of clock drifts.
98
99On the other hand, `performance.timeOrigin` returns the *[Unix time][]* at which the `Performance` object is constructed and relies on the current time exposed through `Date.now()`. That means that it is susceptible to clock drifts that has occurred before the `Performance` object was constructed.
100
101## Supporting w3c-hr-time
102
103The jsdom project (including w3c-hr-time) is a community-driven project maintained by a team of [volunteers](https://github.com/orgs/jsdom/people). You could support us by:
104
105- [Getting professional support for w3c-hr-time](https://tidelift.com/subscription/pkg/npm-w3c-hr-time?utm_source=npm-w3c-hr-time&utm_medium=referral&utm_campaign=readme) as part of a Tidelift subscription. Tidelift helps making open source sustainable for us while giving teams assurances for maintenance, licensing, and security.
106- Contributing directly to the project.
107
108## License
109
110This software is licensed under the MIT license. See LICENSE.md file for more detail.
111
112[HR-TIME]: https://w3c.github.io/hr-time/
113[NAVIGATION-TIMING]: https://w3c.github.io/navigation-timing/
114[PERFORMANCE-TIMELINE]: https://w3c.github.io/performance-timeline/
115[USER-TIMING]: https://w3c.github.io/user-timing/
116[WEBIDL]: https://heycam.github.io/webidl/
117
118[HR-TIME §4]: https://w3c.github.io/hr-time/#dom-domhighrestimestamp
119[HR-TIME §8]: https://w3c.github.io/hr-time/#privacy-security
120
121[`DOMHighResTimeStamp`]: https://w3c.github.io/hr-time/#dom-domhighrestimestamp
122[`Performance`]: https://w3c.github.io/hr-time/#dfn-performance
123[`Performance#now()`]: https://w3c.github.io/hr-time/#dom-performance-now
124[`Performance#timeOrigin`]: https://w3c.github.io/hr-time/#dom-performance-timeorigin
125[`Performance#toJSON()`]: https://w3c.github.io/hr-time/#dom-performance-tojson
126[browser-process-hrtime]: https://www.npmjs.com/package/browser-process-hrtime
127[clock drift]: https://en.wikipedia.org/wiki/Clock_drift
128[global monotonic clock]: https://w3c.github.io/hr-time/#dfn-global-monotonic-clock
129[Unix time]: https://en.wikipedia.org/wiki/Unix_time
130[webidl2js]: https://github.com/jsdom/webidl2js
Note: See TracBrowser for help on using the repository browser.