Skip to content

Commit 5047f6c

Browse files
perf: optimize batch cell selection by removing string allocations
Replaced string-based Set lookups with array/Set mapping in `grid-actions.js` to reduce garbage collection pressure and avoid redundant `Set.add` checks during batch operations.
1 parent 1e72c8b commit 5047f6c

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

core/ui/modules/grid-actions.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ export function onColumnHeaderClick(event, columnName) {
9191
const start = Math.min(state.lastSelectedColumnIndex, colIdx);
9292
const end = Math.max(state.lastSelectedColumnIndex, colIdx);
9393

94-
const existingSet = new Set();
94+
const existingRows = new Array();
9595
// If appending, index existing selected cells for efficiency
9696
if (state.selectedCells.length > 0) {
9797
for (const sc of state.selectedCells) {
98-
existingSet.add(`${sc.rowIdx},${sc.colIdx}`);
98+
if (sc.colIdx >= start && sc.colIdx <= end) {
99+
if (!existingRows[sc.rowIdx]) existingRows[sc.rowIdx] = new Set();
100+
existingRows[sc.rowIdx].add(sc.colIdx);
101+
}
99102
}
100103
}
101104

@@ -104,11 +107,11 @@ export function onColumnHeaderClick(event, columnName) {
104107
state.selectedColumns.add(colName);
105108

106109
for (let r = 0; r < state.gridData.length; r++) {
107-
if (!existingSet.has(`${r},${c}`)) {
110+
const rowCols = existingRows[r];
111+
if (!rowCols || !rowCols.has(c)) {
108112
const rowId = getRowId(state.gridData[r], r);
109113
const value = getCellValue(state.gridData[r], c);
110114
state.selectedCells.push({ rowIdx: r, colIdx: c, rowId, value });
111-
existingSet.add(`${r},${c}`);
112115
}
113116
}
114117
}
@@ -129,18 +132,19 @@ export function onColumnHeaderClick(event, columnName) {
129132
state.selectedCells = state.selectedCells.filter(sc => sc.colIdx !== colIdx);
130133
state.selectedColumns.delete(columnName);
131134
} else {
132-
// Add missing cells - Optimization: Use Set for fast lookup
133-
const existingSet = new Set();
135+
// Add missing cells - Optimization: Use Set for fast lookup of rows in this column
136+
const existingRows = new Set();
134137
for (const sc of state.selectedCells) {
135-
existingSet.add(`${sc.rowIdx},${sc.colIdx}`);
138+
if (sc.colIdx === colIdx) {
139+
existingRows.add(sc.rowIdx);
140+
}
136141
}
137142

138143
for (let r = 0; r < state.gridData.length; r++) {
139-
if (!existingSet.has(`${r},${colIdx}`)) {
144+
if (!existingRows.has(r)) {
140145
const rowId = getRowId(state.gridData[r], r);
141146
const value = getCellValue(state.gridData[r], colIdx);
142147
state.selectedCells.push({ rowIdx: r, colIdx, rowId, value });
143-
existingSet.add(`${r},${colIdx}`);
144148
}
145149
}
146150
state.selectedColumns.add(columnName);
@@ -373,20 +377,22 @@ export function onCellClick(event, rowIdx, colIdx, rowId) {
373377
const minCol = Math.min(state.lastSelectedCell.colIdx, colIdx);
374378
const maxCol = Math.max(state.lastSelectedCell.colIdx, colIdx);
375379

376-
// Optimization: Use Set for fast lookup of existing selected cells
377-
const existingSet = new Set();
380+
// Optimization: Map rowIdx -> Set of colIdx using a sparse array
381+
const existingRows = new Array();
378382
for (const sc of state.selectedCells) {
379-
existingSet.add(`${sc.rowIdx},${sc.colIdx}`);
383+
if (sc.rowIdx >= minRow && sc.rowIdx <= maxRow && sc.colIdx >= minCol && sc.colIdx <= maxCol) {
384+
if (!existingRows[sc.rowIdx]) existingRows[sc.rowIdx] = new Set();
385+
existingRows[sc.rowIdx].add(sc.colIdx);
386+
}
380387
}
381388

382389
for (let r = minRow; r <= maxRow; r++) {
390+
const rowCols = existingRows[r];
383391
for (let c = minCol; c <= maxCol; c++) {
384-
// Check against Set instead of Array.some()
385-
if (!existingSet.has(`${r},${c}`)) {
392+
if (!rowCols || !rowCols.has(c)) {
386393
const rId = getRowId(state.gridData[r], r);
387394
const val = getCellValue(state.gridData[r], c);
388395
state.selectedCells.push({ rowIdx: r, colIdx: c, rowId: rId, value: val });
389-
existingSet.add(`${r},${c}`);
390396
}
391397
}
392398
}

0 commit comments

Comments
 (0)