UNPKG

@react-native-ohos/flash-list

Version:

FlashList is a more performant FlatList replacement

88 lines 3.87 kB
"use strict"; /** * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.JSFPSMonitor = void 0; var roundToDecimalPlaces_1 = require("./roundToDecimalPlaces"); /** * Can be used to monitor JS thread performance * Use startTracking() and stopAndGetData() to start and stop tracking */ var JSFPSMonitor = /** @class */ (function () { function JSFPSMonitor() { var _this = this; this.startTime = 0; this.frameCount = 0; this.timeWindow = { frameCount: 0, startTime: 0, }; this.minFPS = Number.MAX_SAFE_INTEGER; this.maxFPS = 0; this.averageFPS = 0; this.clearAnimationNumber = 0; this.updateLoopCompute = function () { _this.frameCount++; var elapsedTime = (Date.now() - _this.startTime) / 1000; _this.averageFPS = elapsedTime > 0 ? _this.frameCount / elapsedTime : 0; _this.timeWindow.frameCount++; var timeWindowElapsedTime = (Date.now() - _this.timeWindow.startTime) / 1000; if (timeWindowElapsedTime >= 1) { var 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(); }; } JSFPSMonitor.prototype.measureLoop = function () { // This looks weird but I'm avoiding a new closure this.clearAnimationNumber = requestAnimationFrame(this.updateLoopCompute); }; JSFPSMonitor.prototype.startTracking = function () { 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(); }; JSFPSMonitor.prototype.stopAndGetData = function () { cancelAnimationFrame(this.clearAnimationNumber); if (this.minFPS === Number.MAX_SAFE_INTEGER) { this.minFPS = this.averageFPS; this.maxFPS = this.averageFPS; } return { minFPS: (0, roundToDecimalPlaces_1.roundToDecimalPlaces)(this.minFPS, 1), maxFPS: (0, roundToDecimalPlaces_1.roundToDecimalPlaces)(this.maxFPS, 1), averageFPS: (0, roundToDecimalPlaces_1.roundToDecimalPlaces)(this.averageFPS, 1), }; }; return JSFPSMonitor; }()); exports.JSFPSMonitor = JSFPSMonitor; //# sourceMappingURL=JSFPSMonitor.js.map