I learning underscorejs _.memoize method, I want to know his applicable scenario. dizzy! dizzy! not to understand the API what is saying.
var fibonacci = _.memoize(function(n) {
return n < 2 ? n: fibonacci(n - 1) + fibonacci(n - 2);
});
I learning underscorejs _.memoize method, I want to know his applicable scenario. dizzy! dizzy! not to understand the API what is saying.
var fibonacci = _.memoize(function(n) {
return n < 2 ? n: fibonacci(n - 1) + fibonacci(n - 2);
});
Memoization creates a function that remembers its results so that it does not have to calculate them again.
For instance, when calculating fibonacci(5):
and so on. You can see that fibonacci(3) is used twice, but it will have the same result each time, so there's no need to calculate it twice. Instead, the memoized fibonacci function only calculates it the first time, and stores the result; whenever fibonacci(3) is used after that, it returns the stored result immediately.
See also What is memoization good for and is it really all that helpful?.