getting elapse time using console.time/endtime

 

By running console.time("your-time-label") and then console.endTime("your-time-label"), you automatically get elapse time of your process.


import request from 'request';
import process from 'node:process';

function exec(i: number): Promise<void>  {
  return new Promise((resolve, reject) => {  
    request.get('http://www.google.com', (err: any, resp: any) => {
      if(err) {
        reject('error');
        return console.error(err);
      }
      console.log("Total bytes received: ", resp.body.length, process.pid, i);  
      resolve(resp.body.length);
      });
  })
}


console.time("test");

const promises = [];
for (let i = 0; i < 5; ++i) {
    promises.push(exec(i));
}

Promise.all(promises)
.then((results) => {
    console.log("All done", results);
    console.timeEnd("test");
})
.catch((e) => {
    // Handle errors here
});

Outputs - shown here - so much easier than process.hrtime as you need to do some calculation too - As seen here.








Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm