Compact memoization comparisons
JavaScript performance comparison
Info
A comparison of compact memoization routines providing the bare minimum functionality necessary for caching.
Preparation code
<script>
function fib(x) {
if (x < 2) return 1;
else return fib(x - 1) + fib(x - 2);
}
// compact memoizer
function memoize1(param) {
this.memoize = this.memoize || {};
return (param in this.memoize) ? this.memoize[param] : this.memoize[param] = fib(param);
}
// using an external cache
var memCache = {};
function memoize2(param, cache) {
cache = cache || {};
return (param in cache) ? cache[param] : cache[param] = fib(param);
}
// stoyan's memoizer
function memoize3(param) {
if (!memoize3.cache) {
memoize3.cache = {};
}
if (!memoize3.cache[param]) {
var result = fib(param); //custom function
memoize3.cache[param] = result;
}
return memoize3.cache[param];
}
var m1,m2,m3 = 0;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Compact memoize.js |
|
pending… |
Stoyan's version |
|
pending… |
With external cache |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments