Open In App

Node.js console.timeEnd() Method

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Node.js program to demonstrate the
// console.timeEnd() method
 
// Sample function
function addCount() {
    let sum = 0; // Variable declaration
    for (let i = 1; i < 100000; i++) {
        sum += i; // Adding i to the sum variable
    }
    return sum; // Return sum value
}
 
// Starts the timer, here default label is used
console.time();
 
addCount(); // function call
 
// Ends the timer and print the time
// taken by the piece of code
console.timeEnd();


Output:

default: 7.517ms

Example 2: The below example illustrates the console.timeEnd() method in Node.js:

javascript




// Node.js program to demonstrate the
// console.timeEnd() method
 
// Sample function
function addCount() {
    let sum = 0; // Variable declaration
    for (let i = 1; i < 100000; i++) {
        sum += i; // Adding i to the sum variable
    }
    return sum; // Return sum value
}
 
let timetaken = "Time taken by addCount function";
 
// Starts the timer, the label value is timetaken
console.time(timetaken);
 
addCount(); // function call
 
// Ends the timer and print the time
// taken by the piece of code
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
Previous
Next
Share your thoughts in the comments

Similar Reads