Submitted by Victor Laszlo (not verified) on Wed, 04/19/2006 - 07:40.
Nice article oliver. I have to say that this restriction:
> The inner function captures the variables from the outer function.
makes me a little suspicious of the fancier solutions you present. It's true that the memoization is caught up in the domain logic, but I have needed to do this rarely enough that it hasn't really bothered me. I'm more wary of memory leaks or ineffeciencies in the underlying runtime when using closures than bugs caused by rewriting a little code to test for the presence of a stored value.
Also, here's another take on your memoization routine that uses arguments.callee instead of closures.
function assignMemoizable( object, func , name ){
var f = function (){
if stored value exists
return from cache
else
return arguments.callee.originalFunction.apply( this , arguments );
}
f.originalFunction = func;
object.prototype[ name ] = f;
}
Oliver Steele lives in Western Massachusetts and commutes to downtown LA, where he is bringing an operating system from handwaving to reality. He was the architect of OpenLaszlo, the author of PyWordNet and other open source projects. His interests include programming languages, knowledge representation, information visualization, and math education. [more]
Nice article oliver. I have to say that this restriction:
> The inner function captures the variables from the outer function.
makes me a little suspicious of the fancier solutions you present. It's true that the memoization is caught up in the domain logic, but I have needed to do this rarely enough that it hasn't really bothered me. I'm more wary of memory leaks or ineffeciencies in the underlying runtime when using closures than bugs caused by rewriting a little code to test for the presence of a stored value.
Also, here's another take on your memoization routine that uses arguments.callee instead of closures.
function assignMemoizable( object, func , name ){
var f = function (){
if stored value exists
return from cache
else
return arguments.callee.originalFunction.apply( this , arguments );
}
f.originalFunction = func;
object.prototype[ name ] = f;
}
That seems like it would work too, no?