Skip to content

Commit 1448b81

Browse files
committed
perf: convert progress formatter from HTML string to HTMLElement
Eliminates innerHTML parsing in Find search by returning DOM elements directly. _runFormatterForColumn now reads .textContent instead of parsing HTML via innerHTML for every progress column cell.
1 parent 17497a8 commit 1448b81

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

log-viewer/src/tabulator/format/Progress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function progressFormatter(
99
cell: CellComponent,
1010
formatterParams: ProgressParams,
1111
_onRendered: EmptyCallback,
12-
) {
12+
): string | HTMLElement {
1313
const value = cell.getValue() ?? 0;
1414
const totalVal = formatterParams.totalValue ?? 0;
1515

log-viewer/src/tabulator/format/ProgressComponent.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ type ProgressOptions = {
77
precision?: number;
88
};
99

10-
export function progressComponent(value: number, totalValue: number, options: ProgressOptions) {
10+
export function progressComponent(
11+
value: number,
12+
totalValue: number,
13+
options: ProgressOptions,
14+
): string | HTMLElement {
1115
const { showPercentageText = true, precision = 2 } = options;
1216

1317
const roundedValue = (value || 0).toFixed(precision);
@@ -17,18 +21,32 @@ export function progressComponent(value: number, totalValue: number, options: Pr
1721
const percentComplete =
1822
totalValue !== 0 ? (Math.round((value / totalValue) * 100) / 100) * 100 : 0;
1923

20-
const percentageText = showPercent ? `(${percentComplete.toFixed(2)}%)` : '';
24+
const wrapper = document.createElement('div');
25+
wrapper.className = 'progress-wrapper';
2126

22-
const progressBarElem = `${percentComplete ? `<div class="progress-bar" style="width: ${percentComplete}%;"></div>` : ''}`;
23-
const progressBarTextElem = `<div class="progress-bar__text">
24-
<span>${roundedValue}</span>
25-
${showPercent ? `<span class="progress-bar__text__percent">${percentageText}</span>` : ''}
26-
</div>`;
27+
if (percentComplete) {
28+
const bar = document.createElement('div');
29+
bar.className = 'progress-bar';
30+
bar.style.width = `${percentComplete}%`;
31+
wrapper.appendChild(bar);
32+
}
2733

28-
return `<div class="progress-wrapper">
29-
${progressBarElem}
30-
${progressBarTextElem}
31-
</div>`;
34+
const textEl = document.createElement('div');
35+
textEl.className = 'progress-bar__text';
36+
37+
const valueSpan = document.createElement('span');
38+
valueSpan.textContent = roundedValue;
39+
textEl.appendChild(valueSpan);
40+
41+
if (showPercent) {
42+
const pctSpan = document.createElement('span');
43+
pctSpan.className = 'progress-bar__text__percent';
44+
pctSpan.textContent = `(${percentComplete.toFixed(2)}%)`;
45+
textEl.appendChild(pctSpan);
46+
}
47+
48+
wrapper.appendChild(textEl);
49+
return wrapper;
3250
}
3351

3452
return roundedValue;

log-viewer/src/tabulator/format/ProgressMS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function progressFormatterMS(
99
cell: CellComponent,
1010
formatterParams: ProgressParams,
1111
_onRendered: EmptyCallback,
12-
) {
12+
): string | HTMLElement {
1313
const value = (cell.getValue() || 0) / 1_000_000;
1414
const totalVal = formatterParams.totalValue ?? 0;
1515
const totalValAsMs = totalVal > 0 ? totalVal / 1_000_000 : 0;

0 commit comments

Comments
 (0)