Skip to content

Commit 4bc6c00

Browse files
committed
fix: Use Map for O(1) column lookups in table width calculations
1 parent 8c37834 commit 4bc6c00

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/table/use-column-widths.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
3+
import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
44

55
import { useResizeObserver, useStableCallback } from '@cloudscape-design/component-toolkit/internal';
66
import { getLogicalBoundingClientRect } from '@cloudscape-design/component-toolkit/internal';
@@ -35,12 +35,12 @@ function readWidths(
3535
}
3636

3737
function updateWidths(
38-
visibleColumns: readonly ColumnWidthDefinition[],
38+
columnById: Map<PropertyKey, ColumnWidthDefinition>,
3939
oldWidths: Map<PropertyKey, number>,
4040
newWidth: number,
4141
columnId: PropertyKey
4242
) {
43-
const column = visibleColumns.find(column => column.id === columnId);
43+
const column = columnById.get(columnId);
4444
let minWidth = DEFAULT_COLUMN_WIDTH;
4545
if (typeof column?.width === 'number' && column.width < DEFAULT_COLUMN_WIDTH) {
4646
minWidth = column?.width;
@@ -83,6 +83,12 @@ export function ColumnWidthsProvider({ visibleColumns, resizableColumns, contain
8383
const containerWidthRef = useRef(0);
8484
const [columnWidths, setColumnWidths] = useState<null | Map<PropertyKey, number>>(null);
8585

86+
// Pre-build a Map for column lookups
87+
const columnById = useMemo(
88+
() => new Map(visibleColumns.map(column => [column.id, column])),
89+
[visibleColumns]
90+
);
91+
8692
const cellsRef = useRef(new Map<PropertyKey, HTMLElement>());
8793
const stickyCellsRef = useRef(new Map<PropertyKey, HTMLElement>());
8894
const getCell = (columnId: PropertyKey): null | HTMLElement => cellsRef.current.get(columnId) ?? null;
@@ -96,7 +102,7 @@ export function ColumnWidthsProvider({ visibleColumns, resizableColumns, contain
96102
};
97103

98104
const getColumnStyles = (sticky: boolean, columnId: PropertyKey): ColumnWidthStyle => {
99-
const column = visibleColumns.find(column => column.id === columnId);
105+
const column = columnById.get(columnId);
100106
if (!column) {
101107
return {};
102108
}
@@ -190,7 +196,7 @@ export function ColumnWidthsProvider({ visibleColumns, resizableColumns, contain
190196
}, []);
191197

192198
function updateColumn(columnId: PropertyKey, newWidth: number) {
193-
setColumnWidths(columnWidths => updateWidths(visibleColumns, columnWidths ?? new Map(), newWidth, columnId));
199+
setColumnWidths(columnWidths => updateWidths(columnById, columnWidths ?? new Map(), newWidth, columnId));
194200
}
195201

196202
return (

0 commit comments

Comments
 (0)