memoization is a very useful optimization technique that caches the corresponding results produced by a particular input. Such cumbersome lookups and iterative computations can be reduced as much as possible.
Its basic idea is that for specific inputs, the computed results are returned directly from the data in the cache rather than repeated computations.
Implement memory functions
We can simply interpret memoization as the result of a specific input from a memory function.
Here is a basic simple and practical implementation method, can clearly show the structure of memory function
function Memoize (FN) { return function () { // convert parameter to array var args = Array.prototype.slice.call (arguments); // Create a cache object in the Target function object Fn.cache = fn.c Ache | | {}; // return args in Fn.cache? Fn.cache[args]: Fn.cache[args] = fn.apply (this , args); };}
To make it easier for us to understand memory functions more vividly, we changed the above three-mesh operator to an if statement and added log.
functionmemoize (FN) {return function () { //to convert a parameter to an array varargs =Array.prototype.slice.call (arguments); //Create a cache object in the Target function objectFn.cache = Fn.cache | | {}; //if the cache has corresponding results returned directly from the cache, the cached object is recalculated and stored varresults; if(argsinchfn.cache) {Console.log (' Returns the result from the cache: '); Results=Fn.cache[args]; }Else{Console.log (' Return result from function calculation: '); Results= Fn.cache[args] = fn.apply ( This, args); } returnresults};}
Actual use
Here we apply it to the actual, here is a function to calculate the greatest common divisor. But it's not the point of learning, we just need to know that it receives two numbers and returns them to the greatest common divisor.
// This is a function that calculates greatest common divisor function gcd (A, b) { var T; if (A < b) T = b, B = A, a = T; while (b! = 0) t=b, b= a%b, A=T; return A;} var cachedgcd = Memoize (GCD); Console.log ( CACHEDGCD ( 85, 187)); // return result from function calculation: + console.log (CACHEDGCD ( 85, 187)); // Returns the result from the cache:
In the above code, you can see that the Memoize processed GCD function has the function of memory cache. Not just GCD, memory functions can be used for functions that produce specific results for any particular input.
Simple test
Using time is a simple test of non-memory, and memory when the function is called. There are different results in different browsers, but the overall memory time is shorter than the non-memory.
Console.time (' non-memoized ')
CACHEDGCD (85,187);
Console.timeend (' non-memoized ')
Console.time (' memoized ')
CACHEDGCD (85,187);
Console.timeend (' memoized ')
Since the GCD function involves a relatively small number of operations, this does not clearly reflect the advantages of memory function, imagine, if a function of a large amount of computation. The results returned by the cache will skip these complex operations and return the results quickly.
JavaScript Advanced Knowledge Point--memoization