Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36
Chrome 78
Mac OS X 10.14.5
Desktop
5 years ago
Test name Executions per second
Fib Iterative 79596240.0 Ops/sec
Fib Tommy 691704.4 Ops/sec
HTML Preparation code:
AخA
 
1
<script src="https://www.jsdelivr.com/package/npm/memoizee"></script>
Tests:
  • Fib Iterative

    x
     
    function fibonacci(num){
      var a = 1, b = 0, temp;
      while (num >= 0) {
        temp = a;
        a = a + b;
        b = temp;
        num--;
      }
      return b;
    }
    fibonacci(20);
  • Fib Tommy

     
    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);