()
| 47 | }); |
| 48 | |
| 49 | const calculateStats = (): FrameStats => { |
| 50 | const now = performance.now(); |
| 51 | |
| 52 | while ( |
| 53 | frameTimestamps.length > 0 && |
| 54 | now - frameTimestamps[0] > STATS_WINDOW_MS |
| 55 | ) { |
| 56 | frameTimestamps.shift(); |
| 57 | } |
| 58 | |
| 59 | while (frameIntervals.length > MAX_TIMESTAMPS) { |
| 60 | frameIntervals.shift(); |
| 61 | } |
| 62 | |
| 63 | if (frameTimestamps.length < 2) { |
| 64 | return { |
| 65 | fps: 0, |
| 66 | avgFrameMs: 0, |
| 67 | minFrameMs: 0, |
| 68 | maxFrameMs: 0, |
| 69 | jitter: 0, |
| 70 | droppedFrames: droppedFrameCount, |
| 71 | totalFrames: totalFrameCount, |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | const windowMs = now - frameTimestamps[0]; |
| 76 | const fps = |
| 77 | windowMs > 0 ? ((frameTimestamps.length - 1) / windowMs) * 1000 : 0; |
| 78 | |
| 79 | let avgFrameMs = 0; |
| 80 | let minFrameMs = Number.MAX_VALUE; |
| 81 | let maxFrameMs = 0; |
| 82 | |
| 83 | if (frameIntervals.length > 0) { |
| 84 | let sum = 0; |
| 85 | for (const interval of frameIntervals) { |
| 86 | sum += interval; |
| 87 | minFrameMs = Math.min(minFrameMs, interval); |
| 88 | maxFrameMs = Math.max(maxFrameMs, interval); |
| 89 | } |
| 90 | avgFrameMs = sum / frameIntervals.length; |
| 91 | } |
| 92 | |
| 93 | let jitter = 0; |
| 94 | if (frameIntervals.length > 1) { |
| 95 | let varianceSum = 0; |
| 96 | for (const interval of frameIntervals) { |
| 97 | varianceSum += (interval - avgFrameMs) ** 2; |
| 98 | } |
| 99 | jitter = Math.sqrt(varianceSum / frameIntervals.length); |
| 100 | } |
| 101 | |
| 102 | return { |
| 103 | fps, |
| 104 | avgFrameMs, |
| 105 | minFrameMs: minFrameMs === Number.MAX_VALUE ? 0 : minFrameMs, |
| 106 | maxFrameMs, |
no outgoing calls
no test coverage detected