Skip to content

Instantly share code, notes, and snippets.

@ShigekiKarita
Last active August 29, 2015 14:04
Show Gist options
  • Save ShigekiKarita/173d815ae9bd96596912 to your computer and use it in GitHub Desktop.
Save ShigekiKarita/173d815ae9bd96596912 to your computer and use it in GitHub Desktop.
Memoize in Javacript
// for firefox31
const memoize = func =>
{
let memo = {};
return _ =>
{
if (!(arguments in memo))
{
memo[arguments] = func.apply(this, arguments);
}
return memo[arguments];
};
};
const f = (x, y, z) => x <= y ? y : f(f(x-1, y, z), f(y-1, z, x), f(z-1, x, y));
const memf = memoize(f);
memf(14, 1, 0); // 3260 ms
memf(14, 1, 0); // 0.00849 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment