Test name | Executions per second |
---|---|
Fib Iterative | 79596240.0 Ops/sec |
Fib Tommy | 691704.4 Ops/sec |
<script src="https://www.jsdelivr.com/package/npm/memoizee"></script>
function fibonacci(num){
var a = 1, b = 0, temp;
while (num >= 0) {
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
fibonacci(20);
const memoize = function(func) {
const cache= {}
return (args) => {
const n = args[0]
if (n in cache) {
return cache[n];
} else {
const result = func(n)
cache[n] = result
return result
}
}
};
const fibonacci = memoize((n) => {
if (n <= 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
});
fibonacci(20);