Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use performance.now() when available #195

Merged
merged 1 commit into from Nov 14, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions functions/datetime/microtime.js
@@ -1,13 +1,26 @@
function microtime(get_as_float) {
// discuss at: http://phpjs.org/functions/microtime/
// original by: Paulo Freitas
// improved by Dumitru Uzun (http://duzun.me)
// example 1: timeStamp = microtime(true);
// example 1: timeStamp > 1000000000 && timeStamp < 2000000000
// returns 1: true
// example 2: /^0\.[0-9]{1,6} [0-9]{10,10}$/.test(microtime())
// returns 2: true

var now = new Date()
.getTime() / 1000;
var s = parseInt(now, 10);

return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
if ( typeof performance !== 'undefined' && performance.now ) {
var now = ( performance.now() + performance.timing.navigationStart ) / 1e3;
if(get_as_float) return now;

var s = now | 0; // Math.round(now)
return ( Math.round((now - s) * 1e6) / 1e6 ) + ' ' + s;
}
else {
var now = ( Date.now ? Date.now() : new Date().getTime() ) / 1e3;
if(get_as_float) return now;

var s = now | 0; // Math.round(now)
return ( Math.round((now - s) * 1e3) / 1e3 ) + ' ' + s;
}
}