⚡️ Speed up function areIntersecting by 6%
#36
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.
📄 6% (0.06x) speedup for
areIntersectinginapp/client/src/utils/boxHelpers.ts⏱️ Runtime :
28.1 microseconds→26.6 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 5% runtime improvement (28.1μs → 26.6μs) by eliminating unnecessary intermediate variable allocations.
Key Optimization:
Instead of creating 8 local constants (
l1,r1r,t1,b1,l2,r2r,t2,b2) to store rectangle properties before comparison, the optimized version directly accesses properties in the return statement. This removes the overhead of:Why This Works:
The line profiler reveals that in the original code, the 8 variable assignments consumed 75.366ms total (lines 9-17), while the actual comparison logic took 0ms. The optimized version eliminates this entire assignment overhead, keeping only the comparison operations which JavaScript's JIT compiler can efficiently optimize.
Test Case Performance:
The trade-off is worthwhile: simpler code with less memory pressure and better runtime performance for the common case where rectangle intersection checks are performed frequently in layout algorithms or collision detection systems.
✅ 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-areIntersecting-ml25jac2and push.