⚡️ Speed up function fibonacci by 114,050%
#1093
Closed
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.
📄 114,050% (1,140.50x) speedup for
fibonacciincode_to_optimize_js/fibonacci.js⏱️ Runtime :
33.8 milliseconds→29.6 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 1140x speedup (from 33.8ms to 29.6μs) by replacing the exponential-time recursive algorithm with a linear-time iterative approach for the most common case: non-negative integers.
Key Changes:
Fast Path for Integers (O(n) vs O(2^n)): The optimization adds a check for
Number.isInteger(n) && n >= 0and computes Fibonacci iteratively using just two variables (aandb). This eliminates the exponential explosion of recursive calls that occurs with the naive implementation—for example,fibonacci(20)requires ~21,891 recursive calls in the original but only 19 iterations in the optimized version.Preserves Original Semantics: For edge cases like negative numbers, floats, or string coercions, the code falls back to the original recursive implementation (
slow()), ensuring behavioral compatibility.Why This Works:
Eliminates Redundant Computation: The naive recursion recomputes the same Fibonacci values exponentially many times (e.g.,
fibonacci(2)is called thousands of times when computingfibonacci(20)). The iterative approach computes each value exactly once.Minimal Memory Overhead: Uses O(1) space instead of O(n) call stack depth, preventing stack overflow on larger inputs.
Test Results Show:
Large inputs benefit massively: Performance tests computing
fibonacci(0)throughfibonacci(30)now complete in microseconds instead of seconds. The test expecting completion under 3000ms would pass trivially with the optimized version.Small inputs remain fast: Basic cases (0-10) already execute quickly but still benefit from eliminating function call overhead.
Edge cases preserved: Tests with negative numbers, floats (e.g.,
fibonacci(1.5)), and string coercion (fibonacci('6')) still pass because the fallback path maintains exact original behavior.Impact:
For typical workloads using integer inputs (which represent 100% of canonical Fibonacci use cases), this optimization delivers transformative performance gains while maintaining full backward compatibility for unusual inputs.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-fibonacci-mkhe09u9and push.