Question:
How to detect time in js in microseconds? In particular, I need to summarize the execution time of a piece of code that runs quickly (less than 1 millisecond), but often.
Answer:
To measure time in microseconds (not milliseconds), you need to use the standard function performance.now()
. It returns a real number (time from the start of the process execution) in milliseconds, and the fractional part is, respectively, microseconds.
var time = performance.now();
// некий код
time = performance.now() - time;
console.log('Время выполнения = ', time);
You can also use console.time('mark')
and console.timeEnd('mark')
– but here it will not be possible to output to the console and sum up the received time intervals.