forked from thinkpixellab/pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpsLogger.js
58 lines (52 loc) · 1.01 KB
/
fpsLogger.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
53
54
55
56
57
58
goog.provide('pl.FpsLogger');
/**
@constructor
*/
pl.FpsLogger = function() {
/**
@private
@type {number}
*/
this._lastTick = 0;
/**
@private
@type {number}
*/
this._index = 0;
this._values = [];
/**
@private
@type {number}
*/
this._sum = 0;
/**
@type {number}
*/
this.fps = NaN;
};
/**
@return {number}
**/
pl.FpsLogger.prototype.AddInterval = function() {
var currentTick = goog.now();
if (this._lastTick > 0) {
var secondsPerFrame = currentTick - this._lastTick;
secondsPerFrame /= 1000;
if (this._values.length < pl.FpsLogger.s_size) {
this._values.push(secondsPerFrame);
} else {
this._index = (this._index + 1) % this._values.length;
this._sum -= this._values[this._index];
this._values[this._index] = secondsPerFrame;
}
this._sum += secondsPerFrame;
this.fps = this._values.length / this._sum;
}
this._lastTick = currentTick;
return this.fps;
};
/**
@const
@type {number}
*/
pl.FpsLogger.s_size = 100;