UNPKG

@helpscout/stats

Version:

Easy performance monitoring for JavaScript / React

88 lines (87 loc) 3.27 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./utils"); var Panel_1 = require("./Panel"); function Stats(options) { if (options === void 0) { options = utils_1.defaultOptions; } var _a = __assign({}, utils_1.defaultOptions, options), bottom = _a.bottom, right = _a.right, opacity = _a.opacity, position = _a.position, top = _a.top, left = _a.left, zIndex = _a.zIndex; var container = document.createElement('div'); container.style.cssText = "\n position:" + position + ";\n top:" + utils_1.toPx(top) + ";\n left:" + utils_1.toPx(left) + ";\n bottom:" + utils_1.toPx(bottom) + ";\n right:" + utils_1.toPx(right) + ";\n opacity:" + opacity + ";\n z-index:" + zIndex + ";\n pointer-events: none;\n "; var beginTime = (performance || Date).now(); var prevTime = beginTime; var frames = 0; var nodes = 0; var maxNodes = 0; function addPanel(panel) { container.appendChild(panel.dom); return panel; } function mount() { window.document.body.appendChild(container); } function unmount() { if (container.parentNode) { container.parentNode.removeChild(container); } beginTime = (performance || Date).now(); prevTime = beginTime; frames = 0; nodes = 0; maxNodes = 0; } // @ts-ignore var fpsPanel = addPanel(new Panel_1.default('FPS', '#0f0', '#020')); var memPanel; // @ts-ignore if (window.performance && window.performance.memory) { // @ts-ignore memPanel = addPanel(new Panel_1.default('MB', '#0ff', '#002')); } // @ts-ignore var nodesPanel = addPanel(new Panel_1.default('NODES', '#f08', '#201')); mount(); return { dom: container, mount: mount, unmount: unmount, begin: function () { beginTime = (performance || Date).now(); nodes = utils_1.getTotalNodeCount(); if (nodes > maxNodes) { maxNodes = nodes; } }, end: function () { frames++; var time = (performance || Date).now(); // msPanel.update(time - beginTime, 200) if (time >= prevTime + 1000) { fpsPanel.update((frames * 1000) / (time - prevTime), 100); prevTime = time; frames = 0; if (memPanel) { // @ts-ignore var memory = performance.memory; memPanel.update(memory.usedJSHeapSize / 1048576, memory.jsHeapSizeLimit / 1048576); } nodesPanel.update(nodes, maxNodes * 2); } return time; }, update: function () { beginTime = this.end(); }, }; } exports.default = Stats;