Convert StatTimer
to an ES6 class
This commit is contained in:
parent
6b1eda3e12
commit
50b72dec6e
@ -461,65 +461,57 @@ function isExternalLinkTargetSet() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var StatTimer = (function StatTimerClosure() {
|
class StatTimer {
|
||||||
function rpad(str, pad, length) {
|
constructor(enable = true) {
|
||||||
while (str.length < length) {
|
|
||||||
str += pad;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
function StatTimer() {
|
|
||||||
this.started = Object.create(null);
|
this.started = Object.create(null);
|
||||||
this.times = [];
|
this.times = [];
|
||||||
this.enabled = true;
|
this.enabled = !!enable;
|
||||||
}
|
}
|
||||||
StatTimer.prototype = {
|
|
||||||
time: function StatTimer_time(name) {
|
time(name) {
|
||||||
if (!this.enabled) {
|
if (!this.enabled) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
if (name in this.started) {
|
||||||
|
warn('Timer is already running for ' + name);
|
||||||
|
}
|
||||||
|
this.started[name] = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
timeEnd(name) {
|
||||||
|
if (!this.enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!(name in this.started)) {
|
||||||
|
warn('Timer has not been started for ' + name);
|
||||||
|
}
|
||||||
|
this.times.push({
|
||||||
|
'name': name,
|
||||||
|
'start': this.started[name],
|
||||||
|
'end': Date.now(),
|
||||||
|
});
|
||||||
|
// Remove timer from started so it can be called again.
|
||||||
|
delete this.started[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
let times = this.times;
|
||||||
|
// Find the longest name for padding purposes.
|
||||||
|
let out = '', longest = 0;
|
||||||
|
for (let i = 0, ii = times.length; i < ii; ++i) {
|
||||||
|
let name = times[i]['name'];
|
||||||
|
if (name.length > longest) {
|
||||||
|
longest = name.length;
|
||||||
}
|
}
|
||||||
if (name in this.started) {
|
}
|
||||||
warn('Timer is already running for ' + name);
|
for (let i = 0, ii = times.length; i < ii; ++i) {
|
||||||
}
|
let span = times[i];
|
||||||
this.started[name] = Date.now();
|
let duration = span.end - span.start;
|
||||||
},
|
out += `${span['name'].padEnd(longest)} ${duration}ms\n`;
|
||||||
timeEnd: function StatTimer_timeEnd(name) {
|
}
|
||||||
if (!this.enabled) {
|
return out;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
if (!(name in this.started)) {
|
|
||||||
warn('Timer has not been started for ' + name);
|
|
||||||
}
|
|
||||||
this.times.push({
|
|
||||||
'name': name,
|
|
||||||
'start': this.started[name],
|
|
||||||
'end': Date.now(),
|
|
||||||
});
|
|
||||||
// Remove timer from started so it can be called again.
|
|
||||||
delete this.started[name];
|
|
||||||
},
|
|
||||||
toString: function StatTimer_toString() {
|
|
||||||
var i, ii;
|
|
||||||
var times = this.times;
|
|
||||||
var out = '';
|
|
||||||
// Find the longest name for padding purposes.
|
|
||||||
var longest = 0;
|
|
||||||
for (i = 0, ii = times.length; i < ii; ++i) {
|
|
||||||
var name = times[i]['name'];
|
|
||||||
if (name.length > longest) {
|
|
||||||
longest = name.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (i = 0, ii = times.length; i < ii; ++i) {
|
|
||||||
var span = times[i];
|
|
||||||
var duration = span.end - span.start;
|
|
||||||
out += rpad(span['name'], ' ', longest) + ' ' + duration + 'ms\n';
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return StatTimer;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
CustomStyle,
|
CustomStyle,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user