Implementing Automatic Memoization (returns a closured function) in JavaScript

后端 未结 1 456
面向向阳花
面向向阳花 2021-01-27 20:20

I\'ve read

http://www.sitepoint.com/implementing-memoization-in-javascript/

Automatic Memoization

In all of the previous examples, the functi

相关标签:
1条回答
  • 2021-01-27 21:04

    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.

    0 讨论(0)
提交回复
热议问题