Skip to content

Instantly share code, notes, and snippets.

@mohamedhayibor
Forked from addyosmani/memoize.js
Created December 29, 2015 16:21
Show Gist options
  • Save mohamedhayibor/9706aed91282701b5cd2 to your computer and use it in GitHub Desktop.
Save mohamedhayibor/9706aed91282701b5cd2 to your computer and use it in GitHub Desktop.
memoize.js - a faster JavaScript memoizer
/*
* memoize.js
* by @philogb and @addyosmani
* further optimizations by @mathias
* Released under an MIT license.
*/
function memoize( fn ) {
return function () {
var args = Array.prototype.slice.call(arguments),
hash = "",
i = args.length;
toString = Object.prototype.toString,
callEm = toString.call({}),
currentArg=null;
while(i--){
currentArg = args[i];
hash += (callEm == toString.call(currentArg)) ?
JSON.stringify(currentArg) : currentArg;
fn.memoize || (fn.memoize || {});
//old: 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