|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @oncall memory_lab |
| 9 | + */ |
| 10 | +import {AnyValue} from '../../core/types'; |
| 11 | +import { |
| 12 | + RegisterDataUpdateCallback, |
| 13 | + VisualizerData, |
| 14 | +} from '../dom-element-visualizer-interactive'; |
| 15 | +import {setVisualizerElement} from '../visual-utils'; |
| 16 | + |
| 17 | +function formatBytes(bytes: number): string { |
| 18 | + if (bytes === 0) return '0 B'; |
| 19 | + const k = 1024; |
| 20 | + const sizes = ['B', 'KB', 'MB', 'GB']; |
| 21 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 22 | + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; |
| 23 | +} |
| 24 | + |
| 25 | +export function createStatusText( |
| 26 | + registerDataUpdateCallback: RegisterDataUpdateCallback, |
| 27 | +): HTMLDivElement { |
| 28 | + const statusWidget = document.createElement('div'); |
| 29 | + statusWidget.style.marginLeft = '10px'; |
| 30 | + statusWidget.style.color = 'white'; |
| 31 | + statusWidget.style.fontSize = '10px'; |
| 32 | + statusWidget.style.fontFamily = 'Inter, system-ui, sans-serif'; |
| 33 | + statusWidget.style.overflow = 'hidden'; |
| 34 | + statusWidget.style.whiteSpace = 'nowrap'; |
| 35 | + statusWidget.style.textOverflow = 'ellipsis'; |
| 36 | + statusWidget.textContent = ''; |
| 37 | + |
| 38 | + registerDataUpdateCallback((data: VisualizerData) => { |
| 39 | + const performance = (window as AnyValue).performance; |
| 40 | + const memory = performance?.memory; |
| 41 | + |
| 42 | + const usedHeap = memory?.usedJSHeapSize ?? 0; |
| 43 | + const totalHeap = memory?.totalJSHeapSize ?? 0; |
| 44 | + const totalElements = data.totalDOMElementsCount ?? 0; |
| 45 | + const detachedElements = data.detachedDOMElementsCount ?? 0; |
| 46 | + |
| 47 | + statusWidget.textContent = |
| 48 | + `DOM: ${totalElements} total, ${detachedElements} detached | ` + |
| 49 | + `Heap: ${formatBytes(usedHeap)} / ${formatBytes(totalHeap)}`; |
| 50 | + }); |
| 51 | + |
| 52 | + setVisualizerElement(statusWidget); |
| 53 | + return statusWidget; |
| 54 | +} |
0 commit comments