Skip to content

Instantly share code, notes, and snippets.

@graphicore
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicore/dc8de31f2c480ff3a08a to your computer and use it in GitHub Desktop.
Save graphicore/dc8de31f2c480ff3a08a to your computer and use it in GitHub Desktop.
draft of a generic caching interface
function Constructor(){}
var _p = Constructor.prototype;
p._rawMethod = function(name){}
_p.cachedMethod = memoize(p._rawMethod);
//memoize module
var _CACHE = new WeakMap();
var _pruneProperty = {
get: function () {
return _CACHE.delete.bind(this);
}
}
function memoize(func, keyFunc) {
function cached() {
var cache = _CACHE.get(this)
, args = Array.prototype.slice.call(arguments)
;
if(cache === undefined) {
cache = object.create(null);
_CACHE.set(this, cache);
}
key = keyFunc
? keyFunc.apply(this, args)
: args.join(',')
;
if(!(key in cache))
cache[key] = func.apply(this, args);
return cache[key];
}
/* manual pruning interface*/
Object.defineProperty(cached, 'prune', _pruneProperty)
return cached;
}
exports.memoize = memoize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment