⚡️ Speed up function nextAvailableRowInContainer by 336%#50
Open
codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Open
⚡️ Speed up function nextAvailableRowInContainer by 336%#50codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
nextAvailableRowInContainer by 336%#50codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Conversation
The optimized code achieves a **335% speedup** (from 1.64ms to 375μs) by eliminating expensive intermediate data structure allocations and streamlining the filtering logic. **Key Performance Improvements:** 1. **Removed `omitBy()` allocation**: The original creates an entirely new filtered object by copying all non-modal widgets, which involves object allocation and property copying overhead. 2. **Eliminated `Object.values()` array creation**: Converting the filtered object to an array creates another intermediate data structure that must be allocated and populated. 3. **Replaced `reduce()` with direct iteration**: The `for...in` loop directly accesses widget properties without creating intermediate collections, and the early `continue` statements skip irrelevant widgets immediately. 4. **Consolidated filter conditions**: By combining both filter checks (modal widget type and parent ID matching) in a single conditional with early exit, the code avoids unnecessary property access and comparison operations. **Why This Matters:** The optimization is particularly effective for the test scenarios shown: - **Small datasets** (1-10 widgets): 200-800% faster due to eliminated allocations dominating execution time - **Large datasets** (500+ widgets): 176-896% faster as the single-pass approach scales better, avoiding O(n) memory allocations - **Modal-heavy workloads**: Even with 950 modal widgets, performance remains consistent since the `continue` statement efficiently skips them The direct iteration approach maintains the same O(n) time complexity but with significantly better constant factors, making this optimization beneficial across all workload sizes while preserving identical functional behavior.
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.
📄 336% (3.36x) speedup for
nextAvailableRowInContainerinapp/client/src/entities/Widget/utils.ts⏱️ Runtime :
1.64 milliseconds→375 microseconds(best of10runs)📝 Explanation and details
The optimized code achieves a 335% speedup (from 1.64ms to 375μs) by eliminating expensive intermediate data structure allocations and streamlining the filtering logic.
Key Performance Improvements:
Removed
omitBy()allocation: The original creates an entirely new filtered object by copying all non-modal widgets, which involves object allocation and property copying overhead.Eliminated
Object.values()array creation: Converting the filtered object to an array creates another intermediate data structure that must be allocated and populated.Replaced
reduce()with direct iteration: Thefor...inloop directly accesses widget properties without creating intermediate collections, and the earlycontinuestatements skip irrelevant widgets immediately.Consolidated filter conditions: By combining both filter checks (modal widget type and parent ID matching) in a single conditional with early exit, the code avoids unnecessary property access and comparison operations.
Why This Matters:
The optimization is particularly effective for the test scenarios shown:
continuestatement efficiently skips themThe direct iteration approach maintains the same O(n) time complexity but with significantly better constant factors, making this optimization beneficial across all workload sizes while preserving identical functional behavior.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-nextAvailableRowInContainer-ml28h6t7and push.