Last active
November 15, 2020 02:32
-
-
Save pi0/1476085924f8a2eb1df85929c20cb43f to your computer and use it in GitHub Desktop.
process.hrtime polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] | |
}) | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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]})}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://github.com/cabinjs/browser-hrtime/blob/master/src/index.ts
Minified by https://xem.github.io/terser-online/