⚡️ Speed up function isPerfectSquare by 22%
#1220
Open
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.
📄 22% (0.22x) speedup for
isPerfectSquareincode_to_optimize/js/code_to_optimize_js_cjs/fibonacci.js⏱️ Runtime :
49.1 microseconds→40.4 microseconds(best of250runs)📝 Explanation and details
This optimization achieves a 21% runtime improvement by replacing the floating-point
Math.sqrt()approach with an integer-based Newton's method for computing square roots.Key Changes:
Early termination for edge cases: Added guards for negative numbers (
return false) and zero (return true) before any computation, eliminating unnecessary work for these common cases.Integer square root via Newton's method: Replaced
Math.sqrt()with an iterative integer-only algorithm that uses bit shifts (>> 1for division by 2) and integer arithmetic. This avoids the overhead of floating-point operations entirely.Exact integer comparison: The final check
x * x === nuses pure integer multiplication rather than comparing floating-point square roots withMath.floor(), which is both faster and more precise.Why This Is Faster:
Math.sqrt()operates in the floating-point domain, requiring type conversions and higher computational overhead. The optimized version stays in integer arithmetic throughout.>> 1operation (right shift by 1) is faster than division by 2, as it's a single CPU instruction.Performance Characteristics from Tests:
should efficiently process very large numbersshows 86% speedup, demonstrating that Newton's method converges quickly even for large inputsTrade-offs:
The optimization sacrifices some individual test case performance (perfect squares may take slightly longer in isolated cases) to achieve better overall runtime across the full workload spectrum, particularly excelling at edge cases and large numbers where the function is likely to be most performance-critical.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
fibonacci.test.js::isPerfectSquare returns false for non-perfect squaresfibonacci.test.js::isPerfectSquare returns true for perfect squares🌀 Click to see Generated Regression Tests
📊 Performance Profile
View detailed line-by-line performance analysis
To edit these changes
git checkout codeflash/optimize-isPerfectSquare-ml1y91ssand push.