Comparison of memoization implementations
JavaScript performance comparison
Preparation code
<script>
//memo1
//underscore.js implem
function memoize1(func, hasher) {
var memo = {};
hasher || (hasher = function(value){
return value; //^this could be done better..surely..
});
return function() {
var key = hasher.apply(this, arguments);
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};
//memoize.js - by @addyosmani, @philogb, @mathias
// with a few useful tweaks from @DmitryBaranovsk
function memoize2( fn ) {
return function () {
var args = Array.prototype.slice.call(arguments),
hash = "",
i = args.length;
currentArg = null;
while(i--){
currentArg = args[i];
hash += (currentArg === Object(currentArg)) ?
JSON.stringify(currentArg) : currentArg;
fn.memoize || (fn.memoize = {});
}
return (hash in fn.memoize) ? fn.memoize[hash] :
fn.memoize[hash] = fn.apply( this , args );
};
}
//memo4 - by stevenlevithan
function memoize4(functor, expiration) {
var memo = {};
return function () {
var key = Array.prototype.join.call(arguments, "§");
if (key in memo)
return memo[key];
if (expiration)
setTimeout(function () {delete memo[key];}, expiration);
return memo[key] = functor.apply(this, arguments);
};
}
//@abozhilov
//Based on the @stevenlevithan memoize4 version
function memoize8(fn) {
var memo = {};
return function () {
var key = [].join.call(arguments, '§') + '§';
return (key in memo) ? memo[key] : memo[key] = fn.apply(this, arguments);
};
}
var fib, fiborg;
fiborg = fib = function (x) {
if(x < 2) return 1; else return fib(x-1) + fib(x-2);
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Memo 8 |
|
pending… |
Memo 2 - addy, phil, mathias, dmitry |
|
pending… |
Memo 4 - stevenlevithan |
|
pending… |
abozhilov's mod of memoize 4 |
|
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. Here’s a list of current revisions for this page:
- Revision 1: published by Addy Osmani
- Revision 2: published by Addy Osmani
- Revision 3: published by Mariusz Nowak
- Revision 4: published by Addy Osmani
- Revision 5: published
- Revision 6: published by Mariusz Nowak
- Revision 7: published by Dmitry
- Revision 8: published by Dmitry Baranovskiy
- Revision 9: published by Addy Osmani
- Revision 10: published by Jamie Mason
- Revision 11: published by Jamie Mason
- Revision 12: published by Addy Osmani
- Revision 14: published by Paul Grenier
- Revision 15: published and last updated
- Revision 16: published
- Revision 17: published and last updated
- Revision 18: published by JasonG
- Revision 19: published by JasonG
- Revision 20: published by Asen Bozhilov
- Revision 22: published by jbdemonte
- Revision 23: published by nrn
- Revision 24: published by nrn
- Revision 25: published by nrn
- Revision 26: published
- Revision 27: published by Michael Lange
0 comments