Comparison of memoization implementations (v29)

Revision 29 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=new Uint32Array,
           stringifyJson = JSON.stringify,
           sliceArray = Array.prototype.slice,
u;
        return function(a) {
            var u,foo=cache[a];
            return foo===u?cache[a]=func(a):foo;
        };
    };
  
  var fib1, fib2, fiborg;
  fiborg = function f(x) {
      if(x < 2) return 1; else return f(x-1) + f(x-2);
  }

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

Test runner

Ready to run.

Testing in
TestOps/sec
1
fib1((Math.random()*40)|0);
 
ready
2
fib2((Math.random()*40)|0);
ready
org
fiborg((Math.random()*40)|0);
ready

Revisions

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