Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fibo-algo
(version: 0)
Comparing performance of:
Loop vs Recursive vs Memoization vs Recursive using Map as cache
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> function fiboLoop(num){ var a = 1, b = 0, temp; while (num >= 0){ temp = a; a = a + b; b = temp; num--; } return b; } function fiboRec(num) { if (num <= 1) return 1; return fiboRec(num - 1) + fiboRec(num - 2); } function fiboMemo(num, memo) { memo = memo || {}; if (memo[num]) return memo[num]; if (num <= 1) return 1; return memo[num] = fiboMemo(num - 1, memo) + fiboMemo(num - 2, memo); } function Fib() { this.cache = new Map(); this.calc = function(n) { if (n <= 1) return n; let op1, op2; this.cache.has(n - 1) ? op1 = this.cache.get(n - 1) : this.cache.set(n - 1, op1 = this.calc(n - 1)); this.cache.has(n - 2) ? op2 = this.cache.get(n - 2) : this.cache.set(n - 2, op2 = this.calc(n - 2)); return op1 + op2; }; } var fib = new Fib(); </script>
Tests:
Loop
fiboLoop(20)
Recursive
fiboRec(20)
Memoization
fiboMemo(20)
Recursive using Map as cache
fib.calc(20)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Loop
Recursive
Memoization
Recursive using Map as cache
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is testing three different approaches to calculating the nth Fibonacci number: 1. **Loop**: A simple iterative approach using a while loop to calculate the Fibonacci sequence. 2. **Recursive**: A recursive function-based approach, where each call breaks down the problem into smaller sub-problems until the base case is reached. 3. **Memoization**: An optimization technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again. **Library and Functionality** The benchmark uses a custom `Fib` class with a `calc` method, which implements the recursive approach using a Map as cache. The `Fib` class is not a standard JavaScript library; it's a custom implementation of memoization to optimize the recursive function. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark. **Comparison of Approaches** Here's a brief comparison of the three approaches: 1. **Loop**: This approach has the least overhead, as it only uses basic arithmetic operations and doesn't require any function calls or cache management. * Pros: Simple to implement, low overhead. * Cons: May not be efficient for large inputs due to repeated calculations. 2. **Recursive**: This approach breaks down the problem into smaller sub-problems, but also creates multiple stack frames, leading to increased overhead. * Pros: Can be more intuitive to implement, reduces code duplication. * Cons: Can lead to stack overflow errors for large inputs, has high overhead due to function calls and cache management. 3. **Memoization**: This approach stores the results of expensive function calls and returns the cached result when the same inputs occur again, reducing overhead. * Pros: Efficient for large inputs, reduces function call overhead. * Cons: Requires additional memory to store the cache. **Other Alternatives** If you want to compare these approaches with others, you could consider: 1. **Dynamic Programming**: Similar to memoization, but uses arrays or objects to store intermediate results instead of a Map. 2. **Iterative Approach using Arrays**: Uses arrays to store intermediate results and avoid function calls. 3. **Use of Native JavaScript Functions**: Uses built-in JavaScript functions like `reduce()` or `forEach()` to implement the Fibonacci sequence. These alternatives might offer better performance, simplicity, or ease of implementation, depending on your specific requirements.
Related benchmarks:
Array.reduce vs for loop vs Array.forEach experiments
Loop Testing
Array.reduce vs for loop vs Array.forEach v2
groupConsecutiveNumbers with immutable, large and small arrays
Array find middle
Comments
Confirm delete:
Do you really want to delete benchmark?