bito-code-review[bot] commented on PR #42941:
URL: https://github.com/apache/superset/pull/42941#issuecomment-5234011348

   <!-- Bito Reply -->
   The performance issue identified is correct. The current implementation 
performs a linear search (`data.find`) inside the `colorFn` function, which is 
called for every geographic feature during rendering, leading to O(features × 
rows) complexity.
   
   To resolve this, you can pre-process the data into a Map for O(1) lookups 
before defining `colorFn`.
   
   ### Proposed Fix
   
   In `superset-frontend/plugins/plugin-chart-country-map/src/CountryMap.ts`, 
update the `CountryMap` function to create a lookup map:
   
   ```typescript
     const regionMap = useMemo(() => 
       new Map(data.map(region => [region.country_id, region])),
     [data]);
   
     const colorFn = (feature: GeoFeature): string => {
       if (!feature?.properties) return '#d9d9d9';
       const regionData = regionMap.get(feature.properties.ISO);
   
       if (regionData && formatters?.length > 0) {
         for (const formatter of formatters) {
           const cfColor = formatter.getColorFromValue(regionData.metric);
           if (cfColor) return cfColor;
         }
       }
       return colorMap[feature.properties.ISO] || '#d9d9d9';
     };
   ```
   
   This change ensures that lookups are efficient. Would you like me to check 
the rest of the comments on this PR to see if there are other issues to address?
   
   **superset-frontend/plugins/plugin-chart-country-map/src/CountryMap.ts**
   ```
   const regionMap = useMemo(() => 
       new Map(data.map(region => [region.country_id, region])),
     [data]);
   
     const colorFn = (feature: GeoFeature): string => {
       if (!feature?.properties) return '#d9d9d9';
       const regionData = regionMap.get(feature.properties.ISO);
   
       if (regionData && formatters?.length > 0) {
         for (const formatter of formatters) {
           const cfColor = formatter.getColorFromValue(regionData.metric);
           if (cfColor) return cfColor;
         }
       }
       return colorMap[feature.properties.ISO] || '#d9d9d9';
     };
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to