Created
August 7, 2018 14:27
-
-
Save piti118/4bf7ab243da91fe29ff13ab3f72a1301 to your computer and use it in GitHub Desktop.
This file contains 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
export function memoize(fn) { | |
var cache = null | |
var lastRet = null | |
function cacheHit(oldCache, newCache) { | |
return oldCache.length == newCache.length && | |
oldCache.every((x,i) => x === newCache[i]) | |
} | |
return (...args) => { | |
if(cache===null || !cacheHit(cache, args) ){ | |
//console.log('cache miss') | |
cache = [...args] | |
lastRet = fn(...args) | |
} | |
return lastRet | |
} | |
} | |
//function add(a,b){ | |
// return a+b | |
//} | |
//const madd = memoize(add) | |
//console.log(madd(1,3)) | |
//console.log(madd(1,3)) | |
//console.log(madd(1,4)) | |
//console.log(madd(1,3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment