Skip to content

Instantly share code, notes, and snippets.

@anfedorov
Created November 12, 2009 06:48
Show Gist options
  • Save anfedorov/232666 to your computer and use it in GitHub Desktop.
Save anfedorov/232666 to your computer and use it in GitHub Desktop.
goog.memoize = function(f, opt_serializer) {
var functionHash = goog.getHashCode(f);
var serializer = opt_serializer || goog.memoize.simpleSerializer;
return function() {
// Maps the serialized list of args to the corresponding return value.
var cache = this[goog.memoize.CACHE_PROPERTY_];
if (!cache) {
cache = this[goog.memoize.CACHE_PROPERTY_] = {};
}
var key = serializer(functionHash, arguments);
if (!(key in cache)) {
cache[key] = f.apply(this, arguments);
}
return cache[key];
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment