Skip to content

Instantly share code, notes, and snippets.

@anotherxx
Created September 29, 2017 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anotherxx/c8dc8d199820d2e617148bd9fe69d559 to your computer and use it in GitHub Desktop.
Save anotherxx/c8dc8d199820d2e617148bd9fe69d559 to your computer and use it in GitHub Desktop.
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