⚡️ Speed up function gcdRecursive by 247%#1
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
This optimization achieves a **246% speedup** (92.5μs → 26.7μs) by making two key changes to the GCD computation: **1. Recursion to Iteration Conversion** The original recursive implementation incurs function call overhead on every iteration of the Euclidean algorithm. Each recursive call creates a new stack frame, copies parameters, and manages return addresses. The optimized version replaces this with a simple `while` loop that performs the same mathematical operations without any call overhead. This is particularly impactful for: - **Worst-case inputs** like consecutive Fibonacci numbers (25.5μs → 2.00μs, **1177% faster**) that require many algorithm steps - **Multiple iterations** where coprime or larger numbers need several reduction steps (e.g., 35,12 showing 187% improvement) **2. Math.abs Caching** By storing `Math.abs` in a local constant (`const abs = Math.abs`), the optimization eliminates repeated property lookups on the Math object. In the original code, `Math.abs` is accessed on every recursive call (lines 13-14 showing 72.7% of profiled time). The cached version reduces this to a single lookup per invocation, as evidenced by the optimized profiler showing only 7.4% time on the abs calls. **Why This Works:** - **Call stack elimination**: No recursive overhead means lower memory pressure and faster execution - **Property lookup reduction**: One-time caching of `Math.abs` vs. repeated object property resolution - **Tight loop efficiency**: Modern JavaScript engines optimize simple while loops better than recursive function calls **Test Results Show:** - Basic cases with few iterations: 78-227% faster (e.g., equal numbers, small coprimes) - Zero-handling edge cases: 20-86% faster (immediate termination benefits less from iteration conversion) - Large/worst-case inputs: 711-1177% faster (maximum benefit from eliminating deep recursion) - All 300 random large number iterations pass, confirming mathematical correctness is preserved The optimization maintains identical mathematical behavior while dramatically improving runtime through lower-level execution efficiency.
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.
📄 247% (2.47x) speedup for
gcdRecursiveinjs/src/math/computation.js⏱️ Runtime :
92.5 microseconds→26.7 microseconds(best of250runs)📝 Explanation and details
This optimization achieves a 246% speedup (92.5μs → 26.7μs) by making two key changes to the GCD computation:
1. Recursion to Iteration Conversion
The original recursive implementation incurs function call overhead on every iteration of the Euclidean algorithm. Each recursive call creates a new stack frame, copies parameters, and manages return addresses. The optimized version replaces this with a simple
whileloop that performs the same mathematical operations without any call overhead. This is particularly impactful for:2. Math.abs Caching
By storing
Math.absin a local constant (const abs = Math.abs), the optimization eliminates repeated property lookups on the Math object. In the original code,Math.absis accessed on every recursive call (lines 13-14 showing 72.7% of profiled time). The cached version reduces this to a single lookup per invocation, as evidenced by the optimized profiler showing only 7.4% time on the abs calls.Why This Works:
Math.absvs. repeated object property resolutionTest Results Show:
The optimization maintains identical mathematical behavior while dramatically improving runtime through lower-level execution efficiency.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
math.test.js::GCD and LCM gcdRecursive coprime numbersmath.test.js::GCD and LCM gcdRecursive gcd with negative numbersmath.test.js::GCD and LCM gcdRecursive gcd(0, 5) = 5math.test.js::GCD and LCM gcdRecursive gcd(48, 18) = 6math.test.js::GCD and LCM gcdRecursive gcd(5, 0) = 5🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-gcdRecursive-mky1xs1yand push.