public
Created

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
memoize = function(func, context, single) {
function memoizeArg (argPos, depth) {
var cache = {};
return function () {
if (argPos == 0 && depth == 0) {
argPos = arguments.length;
}
if (argPos <= 0) {
if (!(arguments[argPos] in cache)) {
cache[arguments[argPos]] = func.apply(context, arguments);
}
return cache[arguments[argPos]];
}
else {
if (!(arguments[argPos] in cache)) {
if (single) {
cache = {};
}
cache[arguments[argPos]] = memoizeArg(argPos - 1, depth + 1);
}
return cache[arguments[argPos]].apply(this, arguments);
}
}
}
 
return memoizeArg(func.length - 1, 0);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.