Created
September 29, 2017 05:22
-
-
Save anotherxx/c8dc8d199820d2e617148bd9fe69d559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function memoize(func) | |
{ | |
let cache = {}; | |
return function(x ,y) | |
{ if(Object.keys(cache).length == 0) | |
{ | |
cache.res = func(x,y ); | |
cache.x = x; | |
cache.y = y; | |
console.log('first invoke'); | |
return cache.res; | |
} | |
if( cache.x == x && cache.y == y ) | |
{ | |
console.log('load res from cache'); | |
return cache.res; | |
} | |
cache.res = func(x,y ); | |
cache.x = x; | |
cache.y = y; | |
console.log('load from invoke function...') | |
}; | |
} | |
let | |
handler = memoize(function(x,y ) | |
{ | |
return x + y; | |
}); | |
let | |
res = handler(5 , 10); | |
handler(5 , 10); | |
handler(5 , 10); | |
handler(5 , 11); | |
handler(5 , 11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment