Skip to content

Instantly share code, notes, and snippets.

@piti118
Created August 7, 2018 14:27
Show Gist options
  • Save piti118/4bf7ab243da91fe29ff13ab3f72a1301 to your computer and use it in GitHub Desktop.
Save piti118/4bf7ab243da91fe29ff13ab3f72a1301 to your computer and use it in GitHub Desktop.
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