Browse Source

browser friendly flushTime

gh-953
Rich-Harris 8 years ago
parent
commit
55c23a3f23
  1. 24
      src/utils/flushTime.js

24
src/utils/flushTime.js

@ -1,19 +1,35 @@
const DEBUG = false;
const map = new Map;
let time;
if ( typeof process === 'undefined' ) {
time = function time ( previous ) {
const now = window.performance.now();
return previous ? previous - now : now;
};
} else {
time = function time ( previous ) {
const hrtime = process.hrtime( previous );
if ( previous ) {
return hrtime[0] * 1e3 + hrtime[1] / 1e6;
}
};
}
export function timeStart ( label ) {
if ( !map.has( label ) ) {
map.set( label, {
time: 0
});
}
map.get( label ).start = process.hrtime();
map.get( label ).start = time();
}
export function timeEnd ( label ) {
if ( map.has( label ) ) {
const item = map.get( label );
item.time += toMilliseconds( process.hrtime( item.start ) );
item.time += time( item.start );
}
}
@ -24,10 +40,6 @@ export function flushTime ( log = defaultLog ) {
map.clear();
}
function toMilliseconds ( time ) {
return time[0] * 1e+3 + Math.floor( time[1] * 1e-6 );
}
function defaultLog ( label, time ) {
if ( DEBUG ) {
/* eslint-disable no-console */

Loading…
Cancel
Save