forked from AlexeyKupershtokh/node-perf-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf-time.js
More file actions
25 lines (25 loc) · 789 Bytes
/
perf-time.js
File metadata and controls
25 lines (25 loc) · 789 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
var perfTime = module.exports = function(provider_or_rate, rate) {
if (typeof provider_or_rate == 'function') {
this.provider = provider_or_rate;
this.r = rate ? ~~(rate/1000) : 100;
} else {
this.provider = Date.now;
this.r = provider_or_rate ? ~~(provider_or_rate/1000) : 100;
}
if (!this.r) { this.get = this.provider; }; // disable caching for low rates
this.n = 0;
this.d = null;
this.t = null;
this._reset = this.reset.bind(this);
};
perfTime.prototype.get = function() {
if (this.n) { this.n--; return this.d; }
this.n = this.r;
this.t || (this.t = setTimeout(this._reset, 1));
return this.d = this.provider();
};
perfTime.prototype.reset = function() {
this.n = 0;
this.d = null;
this.t = null;
};