Files
Bubberstation/tgui/packages/common/perf.ts
SkyratBot c98d76bca5 [MIRROR] Tgui: More common components in ts (#27674)
* Tgui: More common components in ts (#83098)

## About The Pull Request
Converts more of common into typescript, with some tests
## Why It's Good For The Game
Typescript,,,,

* Tgui: More common components in ts

---------

Co-authored-by: Jeremiah <42397676+jlsnow301@users.noreply.github.com>
2024-05-15 00:57:58 +02:00

73 lines
1.7 KiB
TypeScript

/**
* Ghetto performance measurement tools.
*
* Uses NODE_ENV to remove itself from production builds.
*
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
const FPS = 60;
const FRAME_DURATION = 1000 / FPS;
// True if Performance API is supported
const supportsPerf = !!window.performance?.now;
// High precision markers
let hpMarkersByName: Record<string, number> = {};
// Low precision markers
let lpMarkersByName: Record<string, number> = {};
/**
* Marks a certain spot in the code for later measurements.
*/
function mark(name: string, timestamp?: number): void {
if (process.env.NODE_ENV !== 'production') {
if (supportsPerf && !timestamp) {
hpMarkersByName[name] = performance.now();
}
lpMarkersByName[name] = timestamp || Date.now();
}
}
/**
* Calculates and returns the difference between two markers as a string.
*
* Use logger.log() to print the measurement.
*/
function measure(markerNameA: string, markerNameB: string): string | undefined {
if (process.env.NODE_ENV === 'production') return;
let markerA = hpMarkersByName[markerNameA];
let markerB = hpMarkersByName[markerNameB];
if (!markerA || !markerB) {
markerA = lpMarkersByName[markerNameA];
markerB = lpMarkersByName[markerNameB];
}
const duration = Math.abs(markerB - markerA);
return formatDuration(duration);
}
/**
* Formats a duration in milliseconds and frames.
*/
function formatDuration(duration: number): string {
const durationInFrames = duration / FRAME_DURATION;
return (
duration.toFixed(duration < 10 ? 1 : 0) +
'ms ' +
'(' +
durationInFrames.toFixed(2) +
' frames)'
);
}
export const perf = {
mark,
measure,
};