Skip to content

Instantly share code, notes, and snippets.

@LiuJi-Jim
Last active October 27, 2015 09:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LiuJi-Jim/9596920 to your computer and use it in GitHub Desktop.
Save LiuJi-Jim/9596920 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
};
}
})();
@stiekel
Copy link

stiekel commented Aug 5, 2015

谢谢,正好需要。

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