precise-now
Version:
Like `performance.now()` but in nanoseconds.
63 lines (17 loc) • 672 B
JavaScript
const nowFunc=()=>{
if(globalThis.process?.hrtime?.bigint!==undefined){
return hrtime.bind(undefined,globalThis.process.hrtime.bigint())
}
if(globalThis.performance?.now!==undefined){
return performanceNow.bind(undefined,globalThis.performance.now())
}
return dateNow.bind(undefined,Date.now())
};
const hrtime=(start)=>
Number(globalThis.process.hrtime.bigint()-start);
const performanceNow=(start)=>
Math.round((globalThis.performance.now()-start)*NANOSECS_TO_MILLISECS);
const dateNow=(start)=>(Date.now()-start)*NANOSECS_TO_MILLISECS;
const NANOSECS_TO_MILLISECS=1e6;
const preciseNow=nowFunc();
export default preciseNow;