Last active
August 29, 2015 14:04
-
-
Save ShigekiKarita/173d815ae9bd96596912 to your computer and use it in GitHub Desktop.
Memoize in Javacript
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
// 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