-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
52 lines (45 loc) · 1.13 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
/**
* @fileoverview Measure the execution time of multiple sections of your code,
* then effortlessly print all the timer results at once to the console.
* @author Anton Ivanov <anton@ivanov.hk>
*/
'use strict';
/**
* Measure the execution time of multiple sections of your code, then
* effortlessly print all the timer results at once to the console.
*/
class DebugTimer {
constructor() {
this.data = {};
}
/**
* Begin the timer.
* @param {string} what - Label, e.g. "loading file".
*/
start(what) {
this.data[what] = {
start: Date.now(),
};
}
/**
* End the timer.
* @param {string} what - Label, e.g. "loading file".
*/
end(what) {
this.data[what].end = Date.now();
this.data[what].total = this.data[what].end - this.data[what].start;
}
/**
* Print the results to the console.
* @returns {str} - The formatted result, same as those printed to console.
*/
print() {
let str = '';
for (let key of Object.keys(this.data)) {
str += `${key}: ${this.data[key].total} ms \r\n`;
}
console.log(str);
return str;
}
}
module.exports = DebugTimer;