Skip to content

Commit 4984ea7

Browse files
committed
Merge pull request locutusjs#195 from duzun/master
use performance.now() when available
2 parents d94f3cc + 9fe5f1f commit 4984ea7

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

functions/datetime/microtime.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
function microtime(get_as_float) {
22
// discuss at: http://phpjs.org/functions/microtime/
33
// original by: Paulo Freitas
4+
// improved by Dumitru Uzun (http://duzun.me)
45
// example 1: timeStamp = microtime(true);
56
// example 1: timeStamp > 1000000000 && timeStamp < 2000000000
67
// returns 1: true
8+
// example 2: /^0\.[0-9]{1,6} [0-9]{10,10}$/.test(microtime())
9+
// returns 2: true
710

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

12-
return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
12+
if ( typeof performance !== 'undefined' && performance.now ) {
13+
var now = ( performance.now() + performance.timing.navigationStart ) / 1e3;
14+
if(get_as_float) return now;
15+
16+
var s = now | 0; // Math.round(now)
17+
return ( Math.round((now - s) * 1e6) / 1e6 ) + ' ' + s;
18+
}
19+
else {
20+
var now = ( Date.now ? Date.now() : new Date().getTime() ) / 1e3;
21+
if(get_as_float) return now;
22+
23+
var s = now | 0; // Math.round(now)
24+
return ( Math.round((now - s) * 1e3) / 1e3 ) + ' ' + s;
25+
}
1326
}

0 commit comments

Comments
 (0)