Measure process time with Node Js?

10,110

Solution 1

Since you are targeting nodejs, you can use process.hrtime as stated in the docs

The process.hrtime() method returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array, where nanoseconds is the remaining part of the real time that can't be represented in second precision.

So you can measure timings up to nanosecond, something that console.time can't, as you can see in your example console.time or Date difference measures 0s.

For example:

const NS_PER_SEC = 1e9;
const MS_PER_NS = 1e-6
const time = process.hrtime();
for (let i; i < 10000; i++) {
  // Just to simulate the process
}
const diff = process.hrtime(time);
console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
console.log(`Benchmark took ${ (diff[0] * NS_PER_SEC + diff[1])  * MS_PER_NS } milliseconds`);

Solution 2

Since I'm using timers in multiple places, I wrote a simple class based on Alex's answer:

const t = new Timer('For Loop')
// your code
t.runtimeMs()     // => 1212.34
t.runtimeMsStr()  // => 'For Loop took 1232.34 milliseconds'

Here's the code:

class Timer {
    // Automatically starts the timer
    constructor(name = 'Benchmark') {
        this.NS_PER_SEC = 1e9;
        this.MS_PER_NS = 1e-6
        this.name = name;
        this.startTime = process.hrtime();
    }

    // returns the time in ms since instantiation
    // can be called multiple times
    runtimeMs() {
        const diff = process.hrtime(this.startTime);
        return (diff[0] * this.NS_PER_SEC + diff[1]) * this.MS_PER_NS;
    }

    // retuns a string: the time in ms since instantiation
    runtimeMsStr() {
        return `${this.name} took ${this.runtimeMs()} milliseconds`;
    }
}

Solution 3

var startTime = new Date();
for(let i; i < 10000; i++) {
// Just to simulate the process
}
var endTime = new Date() - startTime;

You will get the total time that it takes to complete the operation

Share:
10,110
juan garcia
Author by

juan garcia

I am a results-driven worker that loves to learn new technologies, communicate and teach some of them. I enjoy to finish my day of work knowing everything is done in a good quality software. I feel great when trying to make a good software development that looks good, and feels good. It is my passion to see people work with it.

Updated on July 22, 2022

Comments

  • juan garcia
    juan garcia almost 2 years

    I would like to do the following:

    console.time("processA");
    for(let i; i < 10000; i++) {
    // Just to simulate the process
    }
    console.timeEnd("processA");
    

    but I want to capture the time end and use my own logger of information with it.

    Is it possible to handle the console output of the timeEnd?

    How can I measure the time interval of a process in nodejs?

  • juan garcia
    juan garcia about 6 years
    May be using momentjs to substract time could be an option const startTime = moment(Date.now()); //process const totalTime = moment(Date.now()).substract(startTime);
  • juan garcia
    juan garcia about 6 years
    end and begin are undefined, timeSpent is NaNsecs at the end
  • Tom
    Tom almost 4 years
    A typescript utility function to get the milliseconds: const getMsFromHrTime = (diff: ReturnType<HRTime>) => (diff[0] * NS_PER_SEC + diff[1]) * MS_PER_NS;
  • Hunter Kohler
    Hunter Kohler almost 3 years
    For 2020 people coming across this: use process.hrtime.bigint(). The non bigint version has been made legacy.