refactor: improve time complexity of select algorithms#6703
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
|
🦸 Review Hero Summary No issues found. Looks good! |
|
🦸 Review Hero Summary Below consensus threshold (2 unique issues not confirmed by majority)
Local fix prompt (copy to your coding agent)Fix these issues identified on the pull request. One commit per issue fixed.
|
47024eb to
29b5262
Compare
02074cf to
f5f1b1d
Compare
506f073 to
84b7a08
Compare
15fc7cc to
7047e67
Compare
|
@claude review |
There was a problem hiding this comment.
Clean performance refactor (array.includes → Set.has); prior cursor bug and review-hero suggestions have been addressed in subsequent commits, and semantic equivalence checks out.
Extended reasoning...
Overview
This PR is a focused performance refactor across four files, replacing O(n*m) repeated Array.prototype.includes lookups with O(1) Set.prototype.has lookups in: matrix column filtering, percentage builders, and grouped map overlay lookups. checkValueSatisfiesCondition.js only adds JSDoc @type {const} annotations (no behavior change).
Security risks
None. The PR touches internal data shaping and report-building logic. No auth, crypto, permissions, query construction, or user-facing input handling is modified.
Level of scrutiny
Moderate. This touches data calculation logic in the percentage report builder, which is production code, but the changes are mechanical and the two genuine behavior tweaks (every(Array.isArray) validation guard + set1Count === 0 short-circuit) are both behavior-preserving for valid inputs — the short-circuit returns false exactly as the original set1Count > 0 && set1Count === count2Count would, and the validation simply throws earlier on malformed inputs that would have crashed inside includes anyway.
Other factors
The earlier high-severity cursor bug (Sets passed to countAnalyticsThatSatisfyConditions which calls .includes) was fixed in subsequent commits — current code passes the original values/valuesToCompare arrays to that helper while using the Sets only for the local filter. The review-hero suggestion about dead MatrixEntityCell entries in the inclusions Set was also resolved by the cleaner single-Set design with explicit entityLabel extraction. The author explicitly requested re-review (@claude review) after these revisions. Bug hunting found no new issues this run.
There was a problem hiding this comment.
Mechanical array→Set lookup refactor for hot loops; type-only changes in checkValueSatisfiesCondition; prior reviewer findings have been addressed.
Extended reasoning...
Overview
Four files touched: matrixBuilder.ts, checkValueSatisfiesCondition.js, percentagesOfValueCounts.js, and findAccessibleGroupedMapOverlays.js. All changes are O(n²) → O(n) lookup optimizations swapping Array.prototype.includes with Set.prototype.has, plus as const / JSDoc @type {const} annotations that are type-only.
Security risks
None. Changes are purely algorithmic — no auth, crypto, input parsing, or external-boundary code is touched.
Level of scrutiny
Low-to-moderate. These are mechanical refactors with equivalent semantics:
matrixBuilder.ts: the new mergedfieldsSet produces the same exclusion set as the priorentityFieldNames ∪ excludeFields ∪ includeFieldschain (MatrixEntityCell objects in the oldincludeFields.includes(columnName)check never matched a string columnName by reference anyway, so dropping them is safe).percentagesOfValueCounts.js: the Sets are only used for the inner.filter(r => valuesSet.has(r.dataElement))lookup; the original arrayvalues/valuesToCompareis still correctly passed tocountAnalyticsThatSatisfyConditions, so the prior Cursor "Set passed where includes() expected" bug does not apply to the current code.findAccessibleGroupedMapOverlays.jsandcheckValueSatisfiesCondition.js: trivial.
Other factors
Two incidental behavior changes embedded in the refactor: the new !fraction.dataValues.every(Array.isArray) validation guard (defensive — throws earlier on bad input instead of silently misbehaving) and the set1Count === 0 short-circuit (avoids the second filter+count pass; preserves the original return set1Count > 0 && set1Count === count2Count result). Both are safe. Prior reviewer suggestions about MatrixEntityCell entries in the inclusions Set were addressed in commit 176d177. No outstanding unresolved comments.

🦸 Review Hero