Node.js console.profileEnd() Method
Last Updated :
06 Apr, 2023
The console module provides a simple debugging console that is provided by web browsers which export two specific components:
- The console class can be used to write to any Node.js stream. Example: console.log(), console.error(), etc.
- A global console can be used without importing a console. Example: process.stdout, process.stderr, etc.
The console.profileEnd() (Added in v8.0.0) method is an inbuilt application programming interface of the ‘console’ module which does not display anything unless used in the inspector. It actually stops the current JavaScript CPU profiling session if one has been started and prints the report to the Profiles panel of the inspector and if this method is called without a label, the most recently started profile is stopped.
Note: The global console methods are neither consistently synchronous nor consistently asynchronous.
Syntax:
console.profileEnd([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 doesn’t print anything in the console instead, finishes/Ends a JavaScript CPU profile in Inspector.
The Below examples illustrate the use of a console.profileEnd() method in Node.js.
Example 1: Filename: index.js
javascript
console.profile( 'MyLabel' );
for (let i = 0; i < 4; i++) {
console.log( 'Doing task no:' , i);
}
console.profileEnd( 'MyLabel' );
|
Run the 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
javascript
function newProfile(callback) {
try {
for (let i = 1; i < 4; i++) {
console.log( 'Working on task:' , i);
callback();
}
} catch {
console.error( 'error occurred' );
}
}
console.profile( "newProfile()" );
newProfile( function alfa() {
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_profileend_label
Share your thoughts in the comments
Please Login to comment...