Skip to content

Instantly share code, notes, and snippets.

@pi0

pi0/hrtime.js

Last active Nov 15, 2020
Embed
What would you like to do?
process.hrtime polyfill
(function() {
const nowOffset = Date.now();
const now = () => Date.now() - nowOffset;
global.process.hrtime = global.process.hrtime || ((previousTimestamp) => {
const baseNow = Math.floor((Date.now() - now()) * 1e-3)
const clocktime = now() * 1e-3
let seconds = Math.floor(clocktime) + baseNow
let nanoseconds = Math.floor((clocktime % 1) * 1e9)
if (previousTimestamp) {
seconds = seconds - previousTimestamp[0]
nanoseconds = nanoseconds - previousTimestamp[1]
if (nanoseconds < 0) {
seconds--
nanoseconds += 1e9
}
}
return [seconds, nanoseconds]
})
})()
!function(){const o=Date.now(),t=()=>Date.now()-o;global.process.hrtime=global.process.hrtime||(o=>{const e=Math.floor(.001*(Date.now()-t())),n=.001*t();let a=Math.floor(n)+e,l=Math.floor(n%1*1e9);return o&&(a-=o[0],l-=o[1],l<0&&(a--,l+=1e9)),[a,l]})}()
@pi0

This comment has been minimized.

Copy link
Owner Author

@pi0 pi0 commented Nov 10, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment