-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfps.js
More file actions
31 lines (25 loc) · 719 Bytes
/
fps.js
File metadata and controls
31 lines (25 loc) · 719 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
const FPS = () => {
let frameTimes = [];
let lastUpdateTime = performance.now();
let minimum = Infinity;
let maximum = -Infinity;
let average = 0;
const submit = (delta) => {
const now = performance.now();
frameTimes.push(delta);
if (now - lastUpdateTime >= 1000) {
const fpsValues = frameTimes.map(time => 1000 / time);
minimum = Math.min(...fpsValues);
maximum = Math.max(...fpsValues);
average = fpsValues.reduce((a, b) => a + b, 0) / fpsValues.length;
frameTimes = [];
lastUpdateTime = now;
}
}
return {
submit,
get minimum() { return minimum },
get maximum() { return maximum },
get average() { return average },
};
}