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?

Reply

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
4 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.