• Node.js Video Tutorials

NodeJS - console.profile() Method



The Node.js console.profile() method is an inbuilt method of the Console class.

The Node.js console.profile() method will start a JavaScript CPU profile. In simple terms, it begins to record a performance of the profile. console.profileEnd() method is called to stop recording the performance of the profile.

The profile results are displayed in the terminal window as a timeline graph that shows how long each line of code took to execute and which functions were called during execution. The console.profile() method doesn't show any output unless we used it in the inspector.

Syntax

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

console.profile([label]);

Parameters

  • label − We can pass the label for the profile with a name and the input name should be a string.

Return value

This method doesn't return anything, instead; it will start a JavaScript CPU profile in the inspector.

Example

In this example,

  • We are calling the Node.js console.profile() method without passing any name to the label parameter.

  • Then we are performing the subtraction of two numbers.

  • Then we are ending the previously called profile with the console.profileEnd() method.

console.profile();
   var a = 1, b = 6;
   var c = a - b;
   console.log(c)
console.profileEnd();

Output

-5

To understand better execute the above code in the browser's console. Following is the output of the above program in the browser's console.

As we can see from the output below, if we do not pass any label to the method, by default, it will name the profile as 'Profile 1' and so on.

console_profile

Example

In this example,

  • We are calling the Node.js console.profile() method by passing the name to the label parameter.

  • Then we are performing the operation of simple interest.

  • Then we are ending the previously called profile with the console.profileEnd() method with the same label.

console.profile("one");

   var P = 1, R = 1, T = 1;   
   var SI = (P * T * R) / 100;
   console.log(SI);
	
console.profileEnd("one");

Output

0.01

To understand better execute the above code in the browser's console. Following is the output of the above program in the browser's console.

As we can see from the figure below, it started the profile with the label we passed and ended.

profile_one_started

Example

In this example, we are calling the console.profile() method without passing the name to the label parameter. Then we are also calling two different profiles with passing different labels.

console.profile();
console.profile('one');
console.profile('two');
   var a = 2, b = 3;
   var c = a + b;
   if ( a < c ){
      console.log(a + ' is less than ' + c);
   }
   else if (a > b){
      console.log(a + ' is greater than ' + b);
   }
   else{
      console.log(a + ' is equal to ' + c + ' and ' + b);
   }

Output

2 is less than 5

To understand better execute the above code in the browser's console. Following is the output of the above program in the browser's console.

As we can see from the figure below, if we do not pass any label to the method, by default, it will name the profile as 'Profile 1' and so on. The other two profiles are started with their respective label names passed to them.

label_names
nodejs_console_module.htm
Advertisements