Memoization vs. Direct calling (v3)

Revision 3 of this benchmark created by Oleg on


Preparation HTML

<script>

  function memoize2( fn ) {  
      return function () {  
            var hash = arguments[0];
           
            fn.memoize || (fn.memoize = {});  

          return (hash in fn.memoize) ? fn.memoize[hash] :   
              fn.memoize[hash] = fn.apply( this , arguments );  
      };  
  }  
  
    
  //stoyans version
  function memoize7(param){
      if (!memoize7.cache) {
          memoize7.cache = {};
      }
      if (!memoize7.cache[param]) {
          var result = fib(param);
          memoize7.cache[param] = result;
      }
      return memoize7.cache[param];
  }
  
  
     var fib, fiborg, f;
      fiborg = fib = f = function (x) {
          if(x < 2) return 1; else return fib(x-1) + fib(x-2);
      }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Memoize2
fib = memoize2(fiborg);
console.log(fib(25),'test1-norm');
ready
Directly calling
console.log(fib(25));
console.log(fib(25)); //on purpose as a second call is being made. This is to simulate the cache request found in the other tests.
ready
Memoize7
console.log(memoize7(25),'test7-norm');
ready

Revisions

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

  • Revision 1: published by Addy Osmani on
  • Revision 2: published by Addy Osmani on
  • Revision 3: published by Oleg on