Reputation: 634
I have existing code that does the following use of hrtime
before = process.hrtime()
// stuff happens...
after = process.hrtime(before)
latency = (after[0] * 1000000000 + after[1]) / 1000000;
What unit is latency
above?
Upvotes: 1
Views: 4874
Reputation: 634
Turns out the above is in fact milliseconds. Since setTimeout
takes the delay value in milliseconds, it's easy to validate my hrtime
juggling:
const times = []
const delays = [1, 10, 100, 1000, 1001, 1100];
function sleep(ms) {
return new Promise((res) => {
setTimeout(res, ms);
});
}
async function main() {
for (let ms of delays) {
const ping = process.hrtime();
await sleep(ms);
const pong = process.hrtime(ping);
const latency = (pong[0] * 1000000000 + pong[1]) / 1000000;
times.push(latency);
}
console.log(times);
// >>> [2.031091, 11.623596, 101.971041, 1000.554953, 1001.192077, 1104.29338]
}
main();
Upvotes: 4