Skip to content

fix: prevent slow highlight lookups on layers with many features - #659

Open
BRaimbault wants to merge 1 commit into
feat/hover-events-multi-highlightfrom
fix/highlight-perfromance
Open

BRaimbault wants to merge 1 commit into
feat/hover-events-multi-highlightfrom
fix/highlight-perfromance

Conversation

@BRaimbault

@BRaimbault BRaimbault commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Description

What

Replaces the linear scan in Layer#getFeaturesById() with a cached Map index (_featuresById), keyed by both the string uid (properties.id) and the numeric id (f.id) used by Feature State. The index is rebuilt lazily, only when _features is reassigned (tracked via _featuresIndexSource).

Why

getFeaturesById() is called on every hover/select lookup as part of #657's highlight overlay. The old this._features.filter(...) scan is O(n) per call, which becomes a visible hover lag on layers with many features. Caching an id → feature map turns each lookup into O(1) after the first build.

This also fixes a latent correctness bug: Cluster reassigns _features after converting polygons to points for clustering, and the old per-call scan could resolve against whichever array reference happened to be current, including a stale pre-conversion one in some call orderings. The new cache is invalidated by reference equality against _features, so it always reflects the array Cluster last assigned — covered by the added Cluster.spec.js test ("Should resolve against the post-polygon-to-point-conversion features, not a stale reference").

Key changes

  • Layer#getFeaturesById(id) now looks up a cached Map instead of scanning _features; returns at most one match (a get, not a filter) since ids are unique per Feature State's requirements.
  • Cache (_featuresById) is rebuilt only when _features is reassigned (e.g. setFeatures()), detected via a stored reference (_featuresIndexSource).
  • Added tests: index rebuild after setFeatures() replaces the array (Layer.spec.js), and resolution against post-conversion features in Cluster (Cluster.spec.js).

@BRaimbault
BRaimbault force-pushed the fix/highlight-perfromance branch from 784141e to d7e448a Compare July 21, 2026 07:12
@sonarqubecloud

Copy link
Copy Markdown

@BRaimbault
BRaimbault marked this pull request as ready for review July 24, 2026 10:44
@BRaimbault
BRaimbault force-pushed the fix/highlight-perfromance branch from d7e448a to de20237 Compare September 1, 2026 10:40
@BRaimbault
BRaimbault force-pushed the fix/highlight-perfromance branch from de20237 to c467fd0 Compare September 4, 2026 08:45
@sonarqubecloud

sonarqubecloud Bot commented Sep 4, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant