Last active
August 29, 2015 14:08
-
-
Save graphicore/dc8de31f2c480ff3a08a to your computer and use it in GitHub Desktop.
draft of a generic caching interface
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 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