• Node.js Video Tutorials

NodeJS - console.timeStamp() Method



The Node.js console.timeStamp() method is used to add a timestamp entry in the Browser's performance timeline.

This can be helpful for tracking and measuring the execution time of code snippets or functions by creating a marker that can later be referenced within the browser's performance timeline. It takes an optional label parameter which will appear along with the timestamp value in the performance timeline.

The Node.js console.timeStamp() method does not display anything in the output unless we use it in the inspector and this method is added in the Node.js version of v8.0.0. This will add an event with or without the label to the timeline panel of the inspector tab. Now let's look into the syntax and usage of the console.timeStamp() method of Node.js.

Syntax

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

console.timeStamp( [label] );

Parameters

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

Return value

This method doesn't return anything instead; it prints the timestamp at every call in the inspector element of the browser.

Example

In this example,

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

  • Then we are calling the console.timeStamp() method with the same label name as the profile we have started.

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

console.profile('one');
console.log('Welcome to %s', 'Tutorialspoint');
console.timeStamp('one');
console.log('Simply Easy %s at your fingertips', 'Learning');
console.profileEnd('one');

Output

Welcome to Tutorialspoint
Simply Easy Learning at your fingertips

To understand better, execute the above code in the browser's console. Following is the output, if we execute it in the browser's console.

As we can see from the figure below, the profile has started and ended but the timeStamp is not visible. The console.timeStamp() method will add an event with the passed label name to the timeline panel of the inspector element.

timeline

Example

In this example,

  • We are calling the Node.js console.profile() method with a name the label parameter.

  • Then we are calling the console.timeStamp() inside the for loop with the same label name as the profile we have started.

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

console.profile('Two');
for (var i = 0; i<=3; i++) {
	console.timeStamp('Two');
}
console.profileEnd('Two');

Output

//Returns nothing

To understand better, execute the above code in the browser's console. Following is the output, if we execute it in the browser's console.

As we can see in the figure below. So, it will add three events with the passed label name to the timeline panel of the inspector.

timeline_panel
nodejs_console_module.htm
Advertisements