Memoization test

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
   // A RECURSIVE SOLUTION
    
    function fibonacciRecursive(n) {
        if (n <= 1) {
            return 1
        }
        return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2)
    }
  
  const fibonacciMemoFunction = memoizer(fibonacciRecursive)
    
    // USING MEMOIZATION
    
    function fibonacciMemo(n,memo) {
        memo = memo || {}
    
        if (memo[n]) {
            return memo[n]
        }
        if (n <= 1) {
            return 1
        }
    
        return memo[n] = fibonacciMemo(n - 1, memo) + fibonacciMemo(n - 2, memo)
    }
  
  
  function memoizer(fun){
      let cache = {}
      return function (n){
          if(cache[n]){
              return cache[n]
          }else{
          let result = fun(n)
          cache[n] = result
          return result
          }
      }
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Recursive Fibonacci
fibonacciRecursive(20)
pending…
Memoized Fibonacci
fibonacciMemo(20)
pending…
Using Memoizer Function
fibonacciMemoFunction(20)
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments