timer.js
786 Bytes
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
'use strict';
/**
* 计时类
* @example
* let timer = new Timer();
* timer.put('profile');
* timer.put('proflie'); // console output: 12.14
*
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/07
*/
class Timer {
constructor() {
this.timers = {};
}
/**
* 打点计时
*/
put(label) {
let labelTime = this.timers[label];
if (labelTime) {
let duration = process.hrtime(labelTime);
return this._round(duration[1]);
} else {
this.timers[label] = process.hrtime();
}
}
/**
* 格式化成毫秒
* @param {Number} value 纳秒
*/
_round(value) {
return Math.round(value / 10000) / 100;
}
}
module.exports = Timer;