Questa traduzione è incompleta. Collabora alla traduzione di questo articolo dall’originale in lingua inglese.

Il metodo performance.now() restituisce un DOMHighResTimeStamp, misurato in millisecondi.

The timestamp is not actually high-resolution. To mitigate security threats such as Spectre, browsers currently round the results to varying degrees. (Firefox started rounding to 1 millisecond in Firefox 60.) Some browsers may also slightly randomize the timestamp. The precision may improve again in future releases; browser developers are still investigating these timing attacks and how best to mitigate them.

Note: This feature is available in Web Workers.

Il valore restituito rappresenta il tempo trascorso dal time origin.

Tieni a mente i seguenti punti:

  • Negli workers dedicati creati da un contesto Window, il valore nel worker sarà minore di performance.now() nella finestra che ha creato quel worker. Prima era lo stesso di t0 nel contesto principale, ma è stato cambiato
  • Negli workers condivisi o di servizio, til valore nel worker potrebbe essere più alto di quello del contesto principale, perché quella finestra può essere creata dopo quegli workers.

Sintassi

t = performance.now();

Esempio

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");

A differenza di altri timing data disponibili con JavaScript (per esempio Date.now), i timestamps restituiti da Performance.now() non sono limitati a una risoluzione di un millisecondo. Invece, rappresentano il tempo come numeri a virgola mobile con una precisione che può arrivare a un microsecondo.

Inolre, a differenza di Date.now(), il valore restituito da Performance.now() aumenta sempre con costanza, indipendentemente dal clock di sistema (che può essere regolato manualmente o distorto da software come NTP). Altrimenti, performance.timing.navigationStart + performance.now() sarà approssimativamente uguale a Date.now().

Reduced time precision

To offer protection against timing attacks and fingerprinting, the precision of performance.now() might get rounded depending on browser settings.
In Firefox, the privacy.reduceTimerPrecision  preference is enabled by default and defaults to 1ms.

// reduced time precision (1ms) in Firefox 60
performance.now();
// 8781416
// 8781815
// 8782206
// ...


// reduced time precision with `privacy.resistFingerprinting` enabled
performance.now();
// 8865400
// 8866200
// 8866700
// ...

In Firefox, you can also enable privacy.resistFingerprinting — this changes the precision to 100ms or the value of privacy.resistFingerprinting.reduceTimerPrecision.microseconds, whichever is larger.

Specifications

Specification Status Comment
High Resolution Time Level 2
The definition of 'performance.now()' in that specification.
Candidate Recommendation Stricter definitions of interfaces and types.
High Resolution Time
The definition of 'performance.now()' in that specification.
Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
nowChrome Full support 24
Full support 24
No support 21 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 12Firefox Full support 15
Notes
Full support 15
Notes
Notes In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
Notes In Firefox 59 the accuracy was reduced to 2 milliseconds.
IE Full support 10Opera Full support 15Safari Full support 8WebView Android Full support YesChrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support 15
Notes
Full support 15
Notes
Notes In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
Notes In Firefox 59 the accuracy was reduced to 2 milliseconds.
Opera Android No support NoSafari iOS Full support 9Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

See also

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: mdnwebdocs-bot, Rufis01
Ultima modifica di: mdnwebdocs-bot,