1 | function Time() {
|
2 | this.times = {};
|
3 | }
|
4 |
|
5 | Time.prototype.get = function (type) {
|
6 | if (type) {
|
7 | const duration = this.times[type] || [0, 0];
|
8 | return (duration[1] || 0) - duration[0];
|
9 | } else {
|
10 | const children = Object.keys(this.times).map(key => ([key, this.get(key)]));
|
11 | let total = 0;
|
12 | children.forEach(child => {
|
13 | total += child[1]
|
14 | });
|
15 | return {
|
16 | total: total,
|
17 | children: children
|
18 | }
|
19 | }
|
20 | };
|
21 |
|
22 | Time.prototype.start = function (type) {
|
23 | this.times[type] = [Date.now()];
|
24 | };
|
25 |
|
26 | Time.prototype.end = function (type) {
|
27 | this.times[type] = (this.times[type] || [0]).concat(Date.now());
|
28 | };
|
29 |
|
30 |
|
31 | exports = module.exports = Time;
|