@react-native-ohos/flash-list
Version:
FlashList is a more performant FlatList replacement
99 lines (88 loc) • 3.27 kB
text/typescript
/**
* MIT License
*
* Copyright (C) 2024 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { roundToDecimalPlaces } from "./roundToDecimalPlaces";
/**
* Can be used to monitor JS thread performance
* Use startTracking() and stopAndGetData() to start and stop tracking
*/
export class JSFPSMonitor {
private startTime = 0;
private frameCount = 0;
private timeWindow = {
frameCount: 0,
startTime: 0,
};
private minFPS = Number.MAX_SAFE_INTEGER;
private maxFPS = 0;
private averageFPS = 0;
private clearAnimationNumber = 0;
private measureLoop() {
// This looks weird but I'm avoiding a new closure
this.clearAnimationNumber = requestAnimationFrame(this.updateLoopCompute);
}
private updateLoopCompute = () => {
this.frameCount++;
const elapsedTime = (Date.now() - this.startTime) / 1000;
this.averageFPS = elapsedTime > 0 ? this.frameCount / elapsedTime : 0;
this.timeWindow.frameCount++;
const timeWindowElapsedTime =
(Date.now() - this.timeWindow.startTime) / 1000;
if (timeWindowElapsedTime >= 1) {
const timeWindowAverageFPS =
this.timeWindow.frameCount / timeWindowElapsedTime;
this.minFPS = Math.min(this.minFPS, timeWindowAverageFPS);
this.maxFPS = Math.max(this.maxFPS, timeWindowAverageFPS);
this.timeWindow.frameCount = 0;
this.timeWindow.startTime = Date.now();
}
this.measureLoop();
};
public startTracking() {
if (this.startTime !== 0) {
throw new Error(
"This FPS Monitor has already been run, please create a new instance"
);
}
this.startTime = Date.now();
this.timeWindow.startTime = Date.now();
this.measureLoop();
}
public stopAndGetData(): JSFPSResult {
cancelAnimationFrame(this.clearAnimationNumber);
if (this.minFPS === Number.MAX_SAFE_INTEGER) {
this.minFPS = this.averageFPS;
this.maxFPS = this.averageFPS;
}
return {
minFPS: roundToDecimalPlaces(this.minFPS, 1),
maxFPS: roundToDecimalPlaces(this.maxFPS, 1),
averageFPS: roundToDecimalPlaces(this.averageFPS, 1),
};
}
}
export interface JSFPSResult {
minFPS: number;
maxFPS: number;
averageFPS: number;
}