Oliver Steele has written up some of his implementations of memoization in JavaScript.
He ends up with:
JAVASCRIPT:
-
-
function memoizeConstantMethod(o, p) {
-
var f = o[p], mf;
-
var s = function(v) {return o[p]=v||mf};
-
((mf = function() {
-
(s(function(){return value})).reset = mf.reset;
-
return value = f.call(this);
-
}).reset = s)();
-
}
-
If you have calculations that you would like to cache, memoization may be a good choice for you.


2 Responses to “One-Line JavaScript Memoization”
Trackbacks/Pingbacks
… [Trackback]…
[...] Read More Infos here: utropicmedia.net/blog/one-line-javascript-memoization [...]…
… [Trackback]…
[...] Read More here: utropicmedia.net/blog/one-line-javascript-memoization [...]…