Created
March 30, 2017 15:44
-
-
Save danieldram/6eba933f3cecce4c2d75aaf1fa3930a7 to your computer and use it in GitHub Desktop.
simple memoize sample
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
const memoize = (fn) => { | |
const cache = {} | |
return (...args) => { | |
const key = args.toString() | |
if(cache[key] == undefined){ | |
cache[key] = fn(...args) | |
} | |
return cache[key] | |
} | |
} | |
let x = (num, num2) => num + 10 + num2 | |
x = memoize(x) //Either mutate original function to memoized or could create memoized_x variable | |
var y = x(1,3) | |
console.log(y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment