Skip to content

Instantly share code, notes, and snippets.

@nefarioustim
Created November 30, 2011 22:41
Show Gist options
  • Save nefarioustim/1411516 to your computer and use it in GitHub Desktop.
Save nefarioustim/1411516 to your computer and use it in GitHub Desktop.
JavaScript Memoization
function memoize(fn) {
return function() {
var args = Array.prototype.slice.call(arguments),
hash = "",
i = args.length;
currentArg = null;
while (i--) {
currentArg = args[i];
hash += (currentArg === Object(currentArg)) ?
JSON.stringify(currentArg) : currentArg;
fn.memoize || (fn.memoize = {});
}
return (hash in fn.memoize) ?
fn.memoize[hash] : fn.memoize[hash] = fn.apply(this, args);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment