Created
February 14, 2015 03:13
-
-
Save danielstreit/908e28156dceca0d68c5 to your computer and use it in GitHub Desktop.
a standalone memoize function inspired by lodash
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
function memoize(func, resolver) { | |
var memoized = function() { | |
var cache = memoized.cache; | |
var key = resolver ? | |
resolver.apply(this, arguments) : | |
arguments[0]; | |
if (cache.has(key)) { | |
return cache.get(key); | |
} | |
var result = func.apply(this, arguments); | |
cache.set(key, result); | |
return result; | |
}; | |
// Must have pollyfill available for Map if compatibility for older browsers is desired | |
memoized.cache = new Map(); | |
return memoized; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment