-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
52 lines (44 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict'
const PerformanceNow =
(global.performance && global.performance.now) ||
global.performanceNow ||
global.nativePerformanceNow || (() => { try {
var now = require('fbjs/lib/performanceNow')
} finally { return now }})();
const DEFAULT_LABEL = 'default';
const DEFAULT_PREC = 3;
let counts = {};
let startTimes = {};
const fixed = n => Math.trunc(n) === n ? n + '' : n.toFixed(DEFAULT_PREC);
console.time = console.time || ((label = DEFAULT_LABEL) => { startTimes[label] = PerformanceNow() });
console.timeLog = console.timeLog || ((label = DEFAULT_LABEL, desc) => timeRecord(label, desc));
console.timeEnd = console.timeEnd || ((label = DEFAULT_LABEL) => timeRecord(label, undefined, true));
console.count = console.count || ((label = DEFAULT_LABEL) => {
if (!counts[label]) {
counts[label] = 0;
}
counts[label]++;
console.log(`${label}: ${counts[label]}`);
});
console.countReset = console.countReset || ((label = DEFAULT_LABEL) => {
if (counts[label]) {
counts[label] = 0;
} else {
console.warn(`Count for '${label}' does not exist`);
}
});
function timeRecord(label, desc, final) {
const endTime = PerformanceNow();
const startTime = startTimes[label];
if (startTime) {
const delta = endTime - startTime;
if (desc) {
console.log(`${label}: ${fixed(delta)}ms ${desc}`);
} else {
console.log(`${label}: ${fixed(delta)}ms`);
}
if (final) delete startTimes[label];
} else {
console.warn(`Timer '${label}' does not exist`);
}
}