Example code:
const got = require('got');
async function timeSpent (){ const time = new Date().getTime() await got('***') return new Date().getTime() - time
}I wonder if there is a better way?
15 Answers
It's not required to implement your own timing logic when using got.
The response includes a timings object that collects all millis spent per phase.
You only need to read out timings.phases.total from the response object.
const path = "";
const response = await got.post(path, { body: { username: "john_doe", password: "mypass" }, json: true, headers: { Accept: "application/json", "Content-type": "application/json", },
});
logger.debug({type: 'performance', path, duration: response.timings.phases.total});Reference:
You can go through this way.
console.time('test1');
console.time('test2');
setTimeout( (elem) => { console.log('You want to write something here!!'); console.timeEnd('test2');
}, 2000);
console.timeEnd('test1'); You may use a 3rd Party statman-stopwatch to easily record the total time.
Example
const got = require('got');
const Stopwatch = require('statman-stopwatch');
async function timeSpent (){ const sw = new Stopwatch(); sw.start(); await got('***'); sw.stop(); const delta = sw.read(); return delta;
} performance - is a native way to measure things like this.
const got = require('got');
async function timeSpent (){ var t0 = performance.now(); await got('***') var t1 = performance.now(); console.log("Call to do something took " + (t1 - t0) + " milliseconds."); return t1 - t0;
}Check browser support
let currentDate = moment().format('YYYY/MM/DD HH:mm') let dataCalca = moment(dataValue).format('YYYY/MM/DD HH:mm') let hours = moment .duration(moment(currentDate, 'YYYY/MM/DD HH:mm') .diff(moment(dataCalca, 'YYYY/MM/DD HH:mm')) ).asHours();So you get the numbers as an hour. You can calculate with that