Comparison of memoization implementations (v38)

Revision 38 of this benchmark created by william malo on


Preparation HTML

<script>
  
  function memoize1(func) {
       "use strict";
       var cache={},
           stringifyJson = JSON.stringify,
           sliceArray = Array.prototype.slice,
u;
        return function() {
            var hash = stringifyJson(sliceArray.call(arguments));
            var foo=cache[hash];
            return foo===u?cache[hash]=func.apply(this, arguments):foo;
        };
    };

  function memoize2(func) {
       "use strict";
       var cache={},ret,
           stringifyJson = JSON.stringify,
           sliceArray = Array.prototype.slice,
u;
        if(func.length!=1){ret=function() {
            var hash = stringifyJson(sliceArray.call(arguments));
            var foo=cache[hash];
            return foo===u?cache[hash]=func.apply(this, arguments):foo;
        }}else{ ret=function(a) {
            var u,foo=cache[a];
            return foo===u?cache[a]=func(a):foo;
        }}
return ret
    };
  
  var fib1, fib2, fiborg, numb=10;
  fiborg = function f(x) {
      if(x < 2) return 1; else return f(x-1) + f(x-2);
  }

fib1=memoize1(fiborg);
fib2=memoize2(fiborg);
  
</script>

Teardown


    numb=(Math.random()*40)|0
  

Test runner

Ready to run.

Testing in
TestOps/sec
without argument length detection
fib1(numb);
ready
with argument length detection
fib2(numb);
ready

Revisions

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