Memoization test

JavaScript performance comparison

Revision 4 of this test case created by Justine Philip

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)
  const fibonacciMemoFunctionTwo = memoizerFun(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
          }
      }
  }
  
  function memoizerFun(fun){
      let cache = {}
      return function (n){
          if(cache[n] != undefined){
              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
Memoized Fibonacci
fibonacciMemo(20)
pending…
Using Memoizer Function
fibonacciMemoFunction(20)
pending…
Using MEm fun 2
fibonacciMemoFunctionTwo(20)
pending…
Recursive Fibonacci
fibonacciRecursive(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