Frontend Scrapbook

Notes that make a difference

Measuring time

By admin

on Thu May 07 2020

When we think of measuring the time taken by a process, we might be tempted to use something like this in Javascript.

const t1 = new Date().getTime();
//execution code
const t2 = new Date().getTime();
const diff = t2-t1

In modern browser, we have performance.now(). Instead of depending on the date object which return us a time stamp in UNIX format ( since January 1, 1970 00:00:00 ), performance API, which returns the value relative to the page load provides us a more precise value in orders of magnitude.

const t1 = performance.now();
//execution code
const t2 = performance.now();
const diff = t2-t1

DOMHighResTimeStamp type

W3C created a spec to increase the accuracy on performance APIs to the unit of thousandth of a millisecond, typically 5 micro-seconds. This value represented as a double precision floating point number which describes the number of milliseconds for example, 134.999976362 indicates 134 milliseconds and 999 microseconds.