Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a standalone memoize function inspired by lodash
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