perf: optimize collection selection with indexed lookups #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Addresses O(n × m) performance bottleneck in collection widget rendering. With 20 visible collections and 1000 components, the previous implementation performed ~20,000 array scans. This replaces linear searches with Map-based indexes, reducing per-widget complexity to O(1).
Impact: Course Authors using bulk collection selection in large libraries (100+ components, 10+ collections).
Changes
1. Added
useCollectionIndexinghook (ComponentPickerContext.tsx)collectionToComponentsandcomponentToCollectionsmaps once per search update2. Refactored
AddComponentWidget.tsxbuildCollectionComponents()andcountCollectionHits()helper functions3. Optimized
findCommonCollectionKey(ComponentPickerContext.tsx)Example
Before:
After:
Performance
Supporting information
Implements performance optimization discussed in PR #2 code review for bulk collection selection feature.
Testing instructions
Existing test coverage validates correctness (298 library-authoring tests, 13 ComponentPicker tests, 39 Collection tests).
Other information
Original prompt
Performance Optimization for Collection Selection
Problem
The current implementation in PR #2 has performance bottlenecks when rendering multiple collection widgets:
buildCollectionComponentsruns O(n × m) for each widget - With 20 collections visible, the entirehitsarray is scanned 20 timesAddComponentWidgetrebuilds the same collection→component mappingsThis becomes problematic with:
Solution
Implement indexed collection lookups at the context level to amortize the cost across all widgets.
Changes Required
1. Add
useCollectionIndexinghook toComponentPickerContext.tsxAfter line 102 (after the
selectedCollectionsstate declaration), add this new exported hook:2. Update
AddComponentWidget.tsxto use indexed lookupsStep 2a: Update imports
Add
useCollectionIndexingto the import from ComponentPickerContext:Step 2b: Remove the old helper functions
Delete these functions (lines 25-49):
buildCollectionComponentscountCollectionHitsStep 2c: Replace the collectionData useMemo logic
Replace the existing
collectionDatauseMemo (around lines 67-76) with this optimized version:3. Optional: Optimize
findCommonCollectionKeyinComponentPickerContext.tsxReplace the
findCommonCollectionKeyfunction (lines 133-149) with this Set-based version: