Skip to content
Related Articles

Related Articles

Improve Article

Node.js console.profile() Method

  • Last Updated : 31 Aug, 2020
Geek Week

The console module provides a simple debugging console that is provided by web browsers which export two specific components:

  • A Console class that can be used to write to any Node.js stream. Example: console.log(), console.error(), etc.
  • A global console that can be used without importing console. Example: process.stdout, process.stderr, etc.

The console.profile() (Added in v8.0.0) method is an inbuilt application programming interface of the ‘console‘ module which doesn’t display anything unless used in the inspector. It starts a JavaScript CPU profile with an optional label until console.profile() is called. The profile is then added to the Profile panel of the inspector. 

Note: The global console methods are neither consistently synchronous nor consistently asynchronous.

Syntax:

console.profile([label])

Parameters: This function accepts a single parameter as mentioned above and described below:



  • label <string>: It accepts the label name that is further to be used in the inspector. 

Return Value: It don’t print anything in console instead, starts a JavaScript CPU profile in Inspector.

The Below examples illustrate the use of console.profile() method in Node.js.

Example 1: Filename: index.js




// Node.js program to demonstrate the 
// console.profile() Method
  
// Starting MyLabel console profile
console.profile('MyLabel');
  
// Doing some task
for (var i = 0; i < 4; i++) {
  
  // Printing some task
  console.log('Doing task no:', i);
}
  
// Finishing MyLabel profile
console.profileEnd('MyLabel');

Run index.js file using the following command:

node index.js

Output in Console:

Doing task no: 0
Doing task no: 1
Doing task no: 2
Doing task no: 3

Output in Inspector:

Output in Inspector

Example 2: Filename: index.js




// Node.js program to demonstrate the 
// console.profile() Method
  
// New profile function
function newProfile(callback) {      
  try {
      // Doing some task
      for(var i = 1; i < 4; i++) {
         console.log('Working on task:', i);
         callback();      
      }
    } catch {
  
      // Prints if there is error
      console.error('error occured');
    }
}
  
// Starting newProfile() console profile
console.profile("newProfile()");
  
// Calling newprofile()
newProfile(function alfa() {
  
  // Finishing profile
  console.profileEnd();
});

Run index.js file using the following command:

node index.js

Output in Console:

Working on task: 1
Working on task: 2
Working on task: 3

Output in Inspector:

Output in Inspector

Reference: https://nodejs.org/api/console.html#console_console_profile_label




My Personal Notes arrow_drop_up
Recommended Articles
Page :