Skip to content

Commit cb35f87

Browse files
committed
using precision timer, and limiting the output to precision .1
1 parent 3b49ad4 commit cb35f87

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/Stats.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,35 @@
33
*/
44

55
var Stats = function (options) {
6+
// Use high resolution timing API, if available
7+
window.performance = window.performance || {};
8+
performance.now = performance.now || function() { return new Date().getTime(); };
69

710
options = options || {};
811

912
var barHeight = options.barHeight || 30;
1013
var bars = options.bars || 74;
1114

12-
var startTime = Date.now(), prevTime = startTime;
15+
var startTime = performance.now(), prevTime = startTime;
1316
var ms = 0, msMin = Infinity, msMax = 0;
1417
var fps = 0, fpsMin = Infinity, fpsMax = 0;
1518
var frames = 0, mode = 0;
1619

1720
var container = document.createElement( 'div' );
1821
container.id = 'stats';
1922
container.addEventListener( 'mousedown', function ( event ) { event.preventDefault(); setMode( ++ mode % 2 ); }, false );
20-
container.style.cssText = 'width:' + (bars + 6) + 'px;opacity:0.9;cursor:pointer;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px;text-align:left';
23+
container.style.cssText = 'width:' + (bars + 6) + 'px;opacity:0.9;cursor:pointer;font-family:Consolas,Arial,monospace;font-size:9px;font-weight:bold;line-height:15px;text-align:left';
2124

2225
var fpsDiv = document.createElement( 'div' );
2326
fpsDiv.id = 'fps';
2427
fpsDiv.style.cssText = 'padding:0 0 3px 3px;background-color:#002;color:#0ff';
2528
container.appendChild( fpsDiv );
2629

30+
var fpsMinMax = document.createElement( 'div' );
31+
fpsMinMax.id = 'fpsTextMinMax';
32+
fpsMinMax.style.cssText = 'text-align:right;height:0;padding-right:3px';
33+
fpsDiv.appendChild( fpsMinMax );
34+
2735
var fpsText = document.createElement( 'div' );
2836
fpsText.id = 'fpsText';
2937
fpsText.innerHTML = 'FPS';
@@ -47,6 +55,11 @@ var Stats = function (options) {
4755
msDiv.style.cssText = 'padding:0 0 3px 3px;background-color:#020;display:none;color:#0f0;';
4856
container.appendChild( msDiv );
4957

58+
var msMinMax = document.createElement( 'div' );
59+
msMinMax.id = 'msTextMinMax';
60+
msMinMax.style.cssText = 'text-align:right;height:0;padding-right:3px';
61+
msDiv.appendChild( msMinMax );
62+
5063
var msText = document.createElement( 'div' );
5164
msText.id = 'msText';
5265
msText.innerHTML = 'MS';
@@ -100,30 +113,32 @@ var Stats = function (options) {
100113

101114
begin: function () {
102115

103-
startTime = Date.now();
116+
startTime = performance.now();
104117

105118
},
106119

107120
end: function () {
108121

109-
var time = Date.now();
122+
var time = performance.now();
110123

111124
ms = time - startTime;
112125
msMin = Math.min( msMin, ms );
113126
msMax = Math.max( msMax, ms );
114127

115-
msText.textContent = ms + ' MS (' + msMin + '-' + msMax + ')';
128+
msText.textContent = ms.toFixed(1) + ' MS';
129+
msMinMax.textContent = '(' + msMin.toFixed(1) + '-' + msMax.toFixed(1) + ')'
116130
updateGraph( msGraph, Math.min( barHeight, barHeight - ( ms / 200 ) * barHeight ) );
117131

118132
frames ++;
119133

120134
if ( time > prevTime + 1000 ) {
121135

122-
fps = Math.round( ( frames * 1000 ) / ( time - prevTime ) );
136+
fps = (( frames * 1000 ) / ( time - prevTime )).toFixed(1);
123137
fpsMin = Math.min( fpsMin, fps );
124138
fpsMax = Math.max( fpsMax, fps );
125139

126-
fpsText.textContent = fps + ' FPS (' + fpsMin + '-' + fpsMax + ')';
140+
fpsText.textContent = fps + ' FPS';
141+
fpsMinMax.textContent = '(' + fpsMin + '-' + fpsMax + ')';
127142
updateGraph( fpsGraph, Math.min( barHeight, barHeight - ( fps / 100 ) * barHeight ) );
128143

129144
prevTime = time;

0 commit comments

Comments
 (0)