Skip to content

Instantly share code, notes, and snippets.

@pi0
Last active November 15, 2020 02:32
Show Gist options
  • Save pi0/1476085924f8a2eb1df85929c20cb43f to your computer and use it in GitHub Desktop.
Save pi0/1476085924f8a2eb1df85929c20cb43f to your computer and use it in GitHub Desktop.
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
Copy link
Author

pi0 commented Nov 10, 2020

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