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.

  • 2
    it is worth rephrasing the main text of the question so that it becomes a question and not a note for yourself - Grundy
  • one
    @Grundy Ok. Made. - RussCoder

1 answer 1

To measure time in microseconds (not milliseconds), you must use the standard performance.now() function. It returns a real number (time from the start of the process) 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 the output to the console and summing up the time periods obtained will not succeed.

Read more here .