Node.js 将process.hrtime()转换为毫秒

Node.js 将process.hrtime()转换为毫秒,node.js,benchmarking,microbenchmark,Node.js,Benchmarking,Microbenchmark,我现有的代码执行以下hrtime before = process.hrtime() // stuff happens... after = process.hrtime(before) latency = (after[0] * 1000000000 + after[1]) / 1000000; 上面的延迟单位是多少?事实上上面的单位是毫秒。由于setTimeout以毫秒为单位获取延迟值,因此很容易验证我的hrtime杂耍: const times=[] 常数延迟=[1,1010010

我现有的代码执行以下
hrtime

before = process.hrtime()

// stuff happens...

after = process.hrtime(before)

latency = (after[0] * 1000000000 + after[1]) / 1000000;

上面的延迟单位是多少?

事实上上面的单位是毫秒。由于
setTimeout
以毫秒为单位获取延迟值,因此很容易验证我的
hrtime
杂耍:

const times=[]
常数延迟=[1,101001001100];
功能睡眠(ms){
返回新承诺((res)=>{
设置超时(res,ms);
});
}
异步函数main(){
对于(让ms表示延迟){
const ping=process.hrtime();
等待睡眠(ms);
const pong=进程.hr时间(ping);
常数延迟=(pong[0]*100000000+pong[1])/1000000;
推送次数(延迟);
}
console.log(次);
// >>> [2.031091, 11.623596, 101.971041, 1000.554953, 1001.192077, 1104.29338]
}
main();