schangxiang@126.com
2025-09-18 49a51c068d62084bc4c3e77c4be94a20de556c4a
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
'use strict';
 
const assert = require('assert');
const MAP = Symbol('Timing#map');
const LIST = Symbol('Timing#list');
 
 
class Timing {
 
  constructor() {
    this[MAP] = new Map();
    this[LIST] = [];
  }
 
  start(name) {
    if (!name) return;
 
    if (this[MAP].has(name)) this.end(name);
 
    const start = Date.now();
    const item = {
      name,
      start,
      end: undefined,
      duration: undefined,
      pid: process.pid,
      index: this[LIST].length,
    };
    this[MAP].set(name, item);
    this[LIST].push(item);
    return item;
  }
 
  end(name) {
    if (!name) return;
    assert(this[MAP].has(name), `should run timing.start('${name}') first`);
 
    const item = this[MAP].get(name);
    item.end = Date.now();
    item.duration = item.end - item.start;
    return item;
  }
 
  toJSON() {
    return this[LIST];
  }
}
 
module.exports = Timing;