Skip to content

Instantly share code, notes, and snippets.

@danielstreit
Created February 14, 2015 03:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielstreit/908e28156dceca0d68c5 to your computer and use it in GitHub Desktop.
Save danielstreit/908e28156dceca0d68c5 to your computer and use it in GitHub Desktop.
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