Skip to content Skip to sidebar Skip to footer

Implementing Automatic Memoization (returns A Closured Function) In Javascript

I've read http://www.sitepoint.com/implementing-memoization-in-javascript/ Automatic Memoization In all of the previous examples, the functions were explicitly modified to add mem

Solution 1:

The memoize call does not alter the fib function, but returns its new, memoized counterpart. In your code, you're calling that one only once, and the original fib function the next time. You need to create one memoized "wrapper", and call that multiple times:

var mFib = memoize(fib);
log(mFib(43));
log(mFib(43));

You could also overwrite the original fib = memoize(fib);, which would have the additional benefit that the recursive calls (which are the interesting ones) will be memoized as well.

Post a Comment for "Implementing Automatic Memoization (returns A Closured Function) In Javascript"