index.js 841 B

12345678910111213141516171819202122232425262728
  1. module.exports = process.hrtime || hrtime
  2. // polyfil for window.performance.now
  3. var performance = global.performance || {}
  4. var performanceNow =
  5. performance.now ||
  6. performance.mozNow ||
  7. performance.msNow ||
  8. performance.oNow ||
  9. performance.webkitNow ||
  10. function(){ return (new Date()).getTime() }
  11. // generate timestamp or delta
  12. // see http://nodejs.org/api/process.html#process_process_hrtime
  13. function hrtime(previousTimestamp){
  14. var clocktime = performanceNow.call(performance)*1e-3
  15. var seconds = Math.floor(clocktime)
  16. var nanoseconds = Math.floor((clocktime%1)*1e9)
  17. if (previousTimestamp) {
  18. seconds = seconds - previousTimestamp[0]
  19. nanoseconds = nanoseconds - previousTimestamp[1]
  20. if (nanoseconds<0) {
  21. seconds--
  22. nanoseconds += 1e9
  23. }
  24. }
  25. return [seconds,nanoseconds]
  26. }