From d2ad8d63084c4cb69f1b72a50a5664de98ab9b5a Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:11:12 +0800 Subject: [PATCH] perf(connections): sort by column keys without building row snapshots --- .../connection/connection-table.tsx | 76 ++++++++++++++----- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/src/components/connection/connection-table.tsx b/src/components/connection/connection-table.tsx index 6270a65e1..7be806ff8 100644 --- a/src/components/connection/connection-table.tsx +++ b/src/components/connection/connection-table.tsx @@ -218,11 +218,51 @@ const getConnectionCellValue = ( } } -const compareConnectionCellValue = ( - field: ColumnField, +type TrafficSortField = 'download' | 'upload' | 'dlSpeed' | 'ulSpeed' +type DisplaySortField = Exclude + +const connectionSortKey = (field: DisplaySortField, row: IConnectionsItem) => { + switch (field) { + case 'host': + return getConnectionHost(row) + case 'process': + return getConnectionProcess(row) + case 'source': + return getConnectionSource(row) + case 'remoteDestination': + return getConnectionDestination(row) + case 'chains': + return formatConnectionChains(row.chains) + case 'rule': + return getConnectionRule(row) + case 'type': + return getConnectionTypeLabel(row) + case 'time': + return getConnectionStartTime(row) + } +} + +const sortByConnectionKey = ( + connections: IConnectionsItem[], + field: DisplaySortField, + direction: number, +) => { + return connections + .map((row) => ({ row, key: connectionSortKey(field, row) })) + .sort((a, b) => { + const order = + typeof a.key === 'number' + ? (Number(a.key) || 0) - (Number(b.key) || 0) + : String(a.key).localeCompare(String(b.key)) + return order * direction + }) + .map(({ row }) => row) +} + +const compareConnectionTraffic = ( + field: TrafficSortField, left: IConnectionsItem, right: IConnectionsItem, - getSnapshot: (row: IConnectionsItem) => TableRowSnapshot, ) => { switch (field) { case 'download': @@ -234,14 +274,6 @@ const compareConnectionCellValue = ( case 'ulSpeed': return (left.curUpload ?? 0) - (right.curUpload ?? 0) } - const leftValue = getConnectionCellValue(field, getSnapshot(left)) - const rightValue = getConnectionCellValue(field, getSnapshot(right)) - - if (typeof leftValue === 'number' || typeof rightValue === 'number') { - return (Number(leftValue) || 0) - (Number(rightValue) || 0) - } - - return String(leftValue ?? '').localeCompare(String(rightValue ?? '')) } const renderCell = ( @@ -597,13 +629,21 @@ export const ConnectionTable = (props: Props) => { const sortedConnections = useMemo(() => { if (!sorting) return connections - const direction = sorting.desc ? -1 : 1 - return [...connections].sort( - (left, right) => - compareConnectionCellValue(sorting.id, left, right, getRowSnapshot) * - direction, - ) - }, [connections, sorting, getRowSnapshot]) + const { id, desc } = sorting + const direction = desc ? -1 : 1 + if ( + id === 'download' || + id === 'upload' || + id === 'dlSpeed' || + id === 'ulSpeed' + ) { + return [...connections].sort( + (left, right) => compareConnectionTraffic(id, left, right) * direction, + ) + } + + return sortByConnectionKey(connections, id, direction) + }, [connections, sorting]) const tableWidth = useMemo( () => visibleColumns.reduce((total, column) => total + column.size, 0),