Node.js console.timeEnd() Method
Last Updated :
11 Apr, 2023
The console.timeEnd() method is the console class of Node.js. This method stops a timer that was previously started by using console.time() method and display the result using stdout.
Syntax:
console.timeEnd( label )
Parameter: This method accepts a single parameter label that holds the string value. If the label is not passed i.e the value of the label is default then it is automatically given to the method. The label can different for different functions or pieces of code.
Return Value: This method displays the label and the time taken by a piece of code.
Example 1: The below example illustrates the console.timeEnd() method in Node.js:
javascript
function addCount() {
let sum = 0;
for (let i = 1; i < 100000; i++) {
sum += i;
}
return sum;
}
console.time();
addCount();
console.timeEnd();
|
Output:
default: 7.517ms
Example 2: The below example illustrates the console.timeEnd() method in Node.js:
javascript
function addCount() {
let sum = 0;
for (let i = 1; i < 100000; i++) {
sum += i;
}
return sum;
}
let timetaken = "Time taken by addCount function" ;
console.time(timetaken);
addCount();
console.timeEnd(timetaken);
|
Output:
Time taken by addCount function: 8.972ms
Reference: https://nodejs.org/docs/latest-v11.x/api/console.html#console_console_timeend_label
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...