Skip to content

Commit 8e5b2c5

Browse files
martinpittmvollmer
authored andcommitted
FIXUP: Speed up sortByStableOrder()
O(n²) → O(n log n). Still not great, but difficult to improve. #22884 (comment)
1 parent abf19bf commit 8e5b2c5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pkg/networkmanager/network-interface.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,16 @@ export const NetworkInterfacePage = ({
929929
});
930930

931931
// Sort each group by stable signal order
932+
// Build a map for O(1) lookups instead of O(n) indexOf
933+
const orderMap = new Map();
934+
stableAPOrder.current.forEach((mac, index) => orderMap.set(mac, index));
935+
932936
const sortByStableOrder = (a, b) => {
933937
const aMAC = a.props.key;
934938
const bMAC = b.props.key;
935-
const aOrder = stableAPOrder.current.indexOf(aMAC);
936-
const bOrder = stableAPOrder.current.indexOf(bMAC);
937-
if (aOrder === -1 || bOrder === -1) {
939+
const aOrder = orderMap.get(aMAC);
940+
const bOrder = orderMap.get(bMAC);
941+
if (aOrder === undefined || bOrder === undefined) {
938942
return a.columns[2].sortKey.localeCompare(b.columns[2].sortKey);
939943
}
940944
return aOrder - bOrder;

0 commit comments

Comments
 (0)