⚡️ Speed up function getNonDraggedWidgets by 521%#46
Open
codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Open
⚡️ Speed up function getNonDraggedWidgets by 521%#46codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
getNonDraggedWidgets by 521%#46codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Conversation
The optimized code achieves a **520% speedup** (1.08ms → 174μs) by replacing an O(n*m) algorithm with an O(n+m) algorithm through a single strategic data structure change. ## Key Optimization **Set-based lookup instead of Array.includes()** The original code used `Array.includes()` inside a `filter()` callback, creating nested iteration: - For each widget in `layout` (n items), it checked if its ID exists in `draggedWidgetIds` array (m items) - This results in O(n*m) complexity with repeated linear scans The optimized version creates a `Set` from dragged widget IDs upfront and uses `Set.has()` for lookups: - Building the Set: O(m) - Filtering with Set lookups: O(n) × O(1) = O(n) - Total complexity: O(n+m) ## Why This Matters **Performance scales dramatically with input size:** - **Small inputs (3-4 widgets)**: 6-35% slower due to Set construction overhead - **Medium inputs (50+ widgets)**: Break-even point where Set overhead is offset - **Large inputs (300-800 widgets)**: 117-1295% faster, with the speedup increasing as data grows The test results show this pattern clearly: - Basic tests with 2-4 widgets: ~15-25% slower (Set overhead dominates) - Performance tests with 300-800 widgets: 117-1295% faster (algorithm efficiency dominates) ## Trade-offs The optimization introduces a small fixed cost for Set construction, making it slightly slower for trivial inputs (2-4 items). However, for realistic workloads with dozens or hundreds of widgets—which is typical in a layout system's drag-and-drop operations—the algorithmic improvement delivers substantial gains. The refactored code maintains functional style (`map` + `filter`) rather than imperative loops, keeping it readable and maintainable while preserving the core performance benefit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 521% (5.21x) speedup for
getNonDraggedWidgetsinapp/client/src/layoutSystems/anvil/utils/layouts/highlights/highlightUtils.ts⏱️ Runtime :
1.08 milliseconds→174 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 520% speedup (1.08ms → 174μs) by replacing an O(n*m) algorithm with an O(n+m) algorithm through a single strategic data structure change.
Key Optimization
Set-based lookup instead of Array.includes()
The original code used
Array.includes()inside afilter()callback, creating nested iteration:layout(n items), it checked if its ID exists indraggedWidgetIdsarray (m items)The optimized version creates a
Setfrom dragged widget IDs upfront and usesSet.has()for lookups:Why This Matters
Performance scales dramatically with input size:
The test results show this pattern clearly:
Trade-offs
The optimization introduces a small fixed cost for Set construction, making it slightly slower for trivial inputs (2-4 items). However, for realistic workloads with dozens or hundreds of widgets—which is typical in a layout system's drag-and-drop operations—the algorithmic improvement delivers substantial gains.
The refactored code maintains functional style (
map+filter) rather than imperative loops, keeping it readable and maintainable while preserving the core performance benefit.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
📊 Performance Profile
View detailed line-by-line performance analysis
To edit these changes
git checkout codeflash/optimize-getNonDraggedWidgets-ml27gfjiand push.