• Node.js Video Tutorials

NodeJS - console.timeEnd() Method



The node.js console.timeEnd() method is used to stop the timer that was started by console.time(). This method returns the time elapsed in milliseconds since its starting point, as well as a corresponding label that was passed as an argument to console.time(). This can be useful for measuring how long it takes for certain operations or functions to complete in your code. It is an inbuilt method of Console class in node.js. This method retrieves the result in milliseconds.

For example, assume a scenario where an operation was performed in 6788 milliseconds. Then console.timeEnd() method will display as "6.788s".

Syntax

Following is the syntax of node.js console.timeEnd() method −

console.timeEnd(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 label as a parameter, it will label as default.

Return value

This method display will display the time taken from the start of the timer to the end timer with the label. If there is no label, it will display as default.

Example

In the example below,

  • We are performing simple interest with its mathematical formula P * T * R / 100.

  • Then we are starting the timer by using the console.time() method and ending the timer by using the Node.js console.timeEnd() method.

  • We are not passing any label to the method, so it will display the timer label as default.

var P = 10, R = 100, T = 6;
console.time();
   var SimpleInterest = (P * T * R) / 100;
console.timeEnd();
console.log("Simple Interest = " + SimpleInterest);

Output

If we compile and run the above program, we get the following as the output. The timer is showing the time taken to perform that particular operation.

default: 0.056ms
Simple Interest = 60

Example

In the following example,

  • We are performing an operation to print a multiplication table for a specified number.

  • Then are starting a timer by using the console.time() method and ending the timer by using the console.timeEnd() method.

  • We are passing the label parameter to the method, so it will display the timer label accordingly.

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

Output

As we can see in the output. The timer is showing the time taken to perform that particular operation.

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

Example

In the example below,

  • We are creating a string array and performing the removal of an element and adding of an element from an array.

  • Then we are starting the timer by using the console.time() method and ending the timer by using the console.timeEnd() method.

  • We are executing both scenarios; one is without a label parameter and another is by passing a label parameter.

console.time();
var directors = ["Rajamouli", "Sukumar", "Prashant Neel",
   "Rishab shetty", "Maniratnam"];
console.time("Time taken to remove");
directors.pop();
console.log(directors);
console.timeEnd("Time taken to remove");
console.time("Time taken to add");
directors.push("Jeethu joseph");
console.log(directors);
console.timeEnd("Time taken to add");
console.timeEnd();

Output

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

[ 'Rajamouli', 'Sukumar', 'Prashant Neel', 'Rishab shetty' ]
Time taken to remove: 2.755ms
[ 'Rajamouli',
  'Sukumar',
  'Prashant Neel',
  'Rishab shetty',
  'Jeethu joseph' ]
Time taken to add: 0.120ms
default: 3.099ms
nodejs_console_module.htm
Advertisements