Memoization test

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function fibonacci(n) {
      return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  }
  Function.prototype.memoized = function(a) {
  
      if (typeof this.cache === "undefined")  this.cache = [];
      if (this.cache[a]) {
          return this.cache[a];
      } else {
          this.cache[a] = this(a);
          return this.cache[a];
      }
  }
  
  Function.prototype.memoize=function() {
    var t=this;
    return function() {
     return t.memoized.apply(t,arguments);
     //return t.memoized(arguments[0]);
    }
  }
  
  
  

};
</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
var memTest= (function fibonacciM(n) {
    return n < 2 ? n : fibonacciM.memoized(n - 1) + fibonacciM.memoized(n - 2);
}).memoize();

memTest(20);
memTest(20);
pending…
Not memoized
fibonacci(20);
fibonacci(20);
pending…
Memoized Half-Assed version
var memTestHalfAssed = (function fibonacciH(n) {
    return n < 2 ? n : fibonacciH(n - 1) + fibonacciH(n - 2);
}).memoize();

memTestHalfAssed(20);
memTestHalfAssed(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