This post will discuss how to measure the execution time of a method in JavaScript.

1. Using performance.now() function

The performance.now() method returns the time elapsed since the time origin, measured in milliseconds. It returns timestamp up to microsecond precision.

The following example demonstrates the usage of the performance.now() method to track the execution time of an operation.

Download Code

 
Alternatively, you can use the performance.now() method from Performance Timing API, which is the same as implemented in modern Web browsers.

Download Code

2. Using Console.time() function

The console.timeEnd() method starts a timer with the specified label. A subsequent call to the console.timeEnd() method with the same label will output milliseconds elapsed since the first call was made.

Download  Run Code

3. Using Date Object

Another plausible way to track how long an operation took is to use the Date.now() method, which returns the total number of milliseconds elapsed since the Unix Epoch. This is demonstrated below:

Download  Run Code

 
Alternatively, you can use the unary plus operator to convert the date returned by the Date constructor into milliseconds.

Download  Run Code

That’s all about measuring the execution time of a method in JavaScript.