--- title: performance.now() slug: Web/API/Performance/now tags: - API - Performance - Prestazioni - Reference - Riferimento - Web Performance API - metodo translation_of: Web/API/Performance/now ---
Il metodo performance.now()
restituisce un {{domxref("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.
{{AvailableInWorkers}}
Il valore restituito rappresenta il tempo trascorso dal time origin.
Tieni a mente i seguenti punti:
performance.now()
nella finestra che ha creato quel worker. Prima era lo stesso di t0
nel contesto principale, ma è stato cambiatot = performance.now();
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()
.
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.
Specification | Status | Comment |
---|---|---|
{{SpecName('Highres Time Level 2', '#dom-performance-now', 'performance.now()')}} | {{Spec2('Highres Time Level 2')}} | Stricter definitions of interfaces and types. |
{{SpecName('Highres Time', '#dom-performance-now', 'performance.now()')}} | {{Spec2('Highres Time')}} | Initial definition |
{{Compat("api.Performance.now")}}