From b0e548923b1f066043216d60c94f2cf95b1e3c76 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 1 Jul 2026 10:32:07 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20map=20pins=20loo?= =?UTF-8?q?kup=20time=20complexity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What: Replaced O(N) Array.find calls with a memoized O(1) Map lookup (pinsById) in territory-workspace.tsx. Added code comments explaining the optimization. Why: The route stops calculation previously ran Array.find inside a map over routeStopIds, leading to O(N*M) time complexity. This causes UI thread blocking on large datasets. Impact: Significantly reduces re-render cost and thread blocking when selecting pins or adding route stops. Lookups are now O(1) instead of O(N). Measurement: Check the React devtools profiler when selecting map pins or modifying route plan; the rendering time for routeStops useMemo will be near zero. Co-authored-by: brycejohnson1417 <257422776+brycejohnson1417@users.noreply.github.com> --- components/territory/territory-workspace.tsx | 44 ++++++++++++++++---- package-lock.json | 1 - 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/components/territory/territory-workspace.tsx b/components/territory/territory-workspace.tsx index 6d2ff49..25a12ba 100644 --- a/components/territory/territory-workspace.tsx +++ b/components/territory/territory-workspace.tsx @@ -570,14 +570,42 @@ export function TerritoryWorkspace({ orgSlug, initialDashboard, territoryConfig const mobileViewAutoSwitchedRef = useRef(initialPreferListView); const pins = data.pins; - const selectedPin = useMemo(() => pins.find((pin) => pin.id === selectedId) ?? null, [pins, selectedId]); - const routeStops = useMemo( - () => - routePlanningEnabled - ? routeStopIds.map((id) => pins.find((pin) => pin.id === id)).filter((pin): pin is TerritoryAccountPin => Boolean(pin)) - : [], - [pins, routePlanningEnabled, routeStopIds], - ); + + // ⚡ Bolt: Performance optimization + // Pre-compute an O(1) Map dictionary to prevent O(N^2) lookups + // during route planning and selection interactions. + const pinsById = useMemo(() => { + const map = new Map(); + for (const pin of pins) { + map.set(pin.id, pin); + } + return map; + }, [pins]); + + const selectedPin = useMemo(() => { + if (!selectedId) { + return null; + } + const pin = pinsById.get(selectedId); + if (pin) { + return pin; + } + return null; + }, [pinsById, selectedId]); + + const routeStops = useMemo(() => { + if (!routePlanningEnabled) { + return []; + } + const stops: TerritoryAccountPin[] = []; + for (const id of routeStopIds) { + const pin = pinsById.get(id); + if (pin) { + stops.push(pin); + } + } + return stops; + }, [pinsById, routePlanningEnabled, routeStopIds]); const mappablePinCount = useMemo(() => pins.filter(hasUsableCoordinates).length, [pins]); const activeFilterCount = Object.values(filters).filter(Boolean).length; const useLiteMarkers = isNarrowViewport || mappablePinCount > 1200; diff --git a/package-lock.json b/package-lock.json index 6115437..cfe04eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4513,7 +4513,6 @@ "version": "2.3.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, "hasInstallScript": true, "license": "MIT", "optional": true,