Skip to content

Measure function execution time in Node JS

This quick post describes how to log the execution time of a JavaScript function. The console object has two methods – time() and timeEnd() that help with analysing performance of your JavaScript / Node Js code. It also helps you log the time taken by a function.

console.time()

Call console.time() with an argument. This starts a timer. The argument you pass is a unique string which needs to be passed to console.timeEnd() to stop the timer. At a point of time, there can be 10000 timers running in parallel.

console.timeLog()

console.timeLog() prints time taken from the point the timer is started.

console.timeEnd()

This method tops a timer that was previously started by calling console.time()

Example

One thing to note here is timeEnd() method outputs the time taken in milliseconds.

console.time("timer");
alert("Click to start");
console.timeLog("timer");
for (let i = 0; i < 5000-; i++) {
  // Your code here
}
console.timeEnd("timer);
See also  Numeric separators in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.