• Node.js Video Tutorials

NodeJS - console.time() Method



The Node.js console.time() method of node.js starts a timer, which we can use to track the time taken by an operation or any function to execute. We can label each timer with a unique name, and we can have up to 10,000 timers running on a specified webpage.

To end the timer we need to call the console.timeEnd() with the same name. The output will be in milliseconds. For example, if the elapsed time is 8942ms, then console.timeEnd() method displays as "8.942s". Let's move into the syntax and usage of the console.time() method of node.js.

Syntax

Following is the syntax of the Node.js console.time() method −

console.time(label);

Parameters

  • label − This can be passed in the method as a parameter with a unique name. We can have different labels for different functions or operations. If we do not pass any parameters to this function, it will consider "default" as the default label.

Return value

This method will not return anything, instead, it starts the timer to count the time taken for a particular operation or a function, and to end the timer we need to call the console.timeEnd() method of node.js.

Example

In the example below,

  • We are creating a function and performing the addition of two variables.

  • Here, we are starting the timer inside the function and also ending inside the function.

  • We haven't passed any parameter (label) in the method, therefore, the label is "default".

function func() {
   console.time();   
   var a = 6, b = 5;
   var c = a + b;  
   console.timeEnd();
   return c;
}
console.log("Result of adding 6 and 5: " + func());

Output

As we can see in the output below, the time taken to perform the operation is returned. As we haven't passed any label inside the method, it labels as default.

default: 0.054ms
Result of adding 6 and 5: 11

Example

In the example below, we are creating a function and performing an operation to print a multiplication table of a particular number up to a specified level. Now, we are passing a label for the timer.

function func() {
   console.time("Time taken")
   var table = 6;
   var length = 5;
   var i = 1;   
   console.log("Multiplication table of : "+ table);
   for(i=1; i <= length; i++)
   console.log(table+" * "+i+" = " +(i * table));
}
func();
console.timeEnd("Time taken")

Output

As we can see in the output below, the time taken to perform the operation is returned. As we passed a label inside the method, so the timer labels with the name accordingly.

Multiplication table of : 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
Time taken: 1.794ms

Example

In the example below,

  • We are creating a function, and inside; we creating an array with string elements

  • We are pushing elements into the array.

  • Then, we are calling the console.time() method without a label parameter and also with a label parameter. So, there are two different timers.

function func() {
   console.time();
      var array = ["Robo", "2.0", "Kabali", "Basha"];
      array.push("Kaala","Darbar");
      console.log(array);   
   console.time("Timer 1")
      array.push("Sivaji", "Jailer");
      console.log(array);
   console.timeEnd("Timer 1");
};
func();
console.timeEnd();

Output

If we compile and run the above program, we get the following as the output. Both the timers are showing the time taken to perform their particular operation.

[ 'Robo', '2.0', 'Kabali', 'Basha', 'Kaala', 'Darbar' ]
[
  'Robo',   '2.0',
  'Kabali', 'Basha',
  'Kaala',  'Darbar',
  'Sivaji', 'Jailer'
]
Timer 1: 0.337ms
default: 7.011ms
nodejs_console_module.htm
Advertisements