Skip to content

Instantly share code, notes, and snippets.

@fuchao2012
Forked from LiuJi-Jim/hrtime.js
Created October 8, 2015 09:35
Show Gist options
  • Save fuchao2012/70e1b31db9de5b7c390a to your computer and use it in GitHub Desktop.
Save fuchao2012/70e1b31db9de5b7c390a to your computer and use it in GitHub Desktop.
HRT(High Resolution Timing) in JavaScript
var hrtime = (function(){
if (typeof window !== 'undefined'){
// browser
if (typeof window.performance !== 'undefined' && typeof performance.now !== 'undefined'){
// support hrt
return function(){
return performance.now();
};
}else{
// oh no..
return function(){
return (new Date()).getTime();
};
}
}else{
// node.js
return function(){
var diff = process.hrtime();
return (diff[0] * 1e9 + diff[1]) / 1e6; // nano second -> ms
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment