I was recently looking into a few javascript design patterns and came across memoization while it looks like a good solution to avoid recalculation of values i can see something wrong with it. say for example this simple code,
function square(num)
{
var result;
if(!square.cache[num]){
console.log("calculating a fresh value...");
result = num*num;
square.cache[num] = result;
}
return square.cache[num];
}
square.cache={}
calling console.log(square(20)) //prints out "calculating a fresh value..." and then the result of the calculation 400,
My real question is what happens when the cache grows so large after subsequent calculations that it takes more time to retrieve a result from the cache than it takes to calculate a fresh value. is there any solution towards this?