-
Notifications
You must be signed in to change notification settings - Fork 0
/
hnl.fps.mjs
58 lines (49 loc) · 1.35 KB
/
hnl.fps.mjs
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
/**
* Module template
*
* Usage:
* fpsCounter().then((fps)=> {
* console.log(fps);
* });
*/
function standardDeviation(arr) {
const n = arr.length;
const mean = arr.reduce((acc, val) => acc + val, 0) / n;
const variance = arr.reduce((acc, val) => acc + (val - mean) ** 2, 0) / n;
return Math.sqrt(variance);
}
const fpsCounter = (function(stabilizationTime = 1500, stabilizationThreshold = 5){
let startTime = Date.now();
let frame = 0;
let fps = 0;
let fpsStable = false;
let fpsValues = [];
function tick(interval = 1000) {
const time = Date.now();
frame++;
if (time - startTime > interval) {
fps = (frame / ((time - startTime) / 1000)).toFixed(1);
startTime = time;
frame = 0;
fpsValues.push(fps);
if (fpsValues.length >= stabilizationTime / 1000) {
const fpsStdev = standardDeviation(fpsValues);
if (fpsStdev < stabilizationThreshold) {
fpsStable = true;
} else {
fpsValues.shift();
}
}
}
window.requestAnimationFrame(() => tick(interval));
}
tick(stabilizationTime/4);
return async function getFps() {
if (!fpsStable) {
await new Promise(resolve => setTimeout(resolve, stabilizationTime));
}
return fps;
}
}());
// Export the Promise object from the module
export default fpsCounter;