Editing Comparison of memoization implementations This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script> //memo6 - by @madrobby/thomas fuchs Function.prototype.cached = function(){ var self = this, cache = {}; return function(arg){ if(arg in cache) { //console.log('Cache hit for '+arg); return cache[arg]; } else { //console.log('Cache miss for '+arg); return cache[arg] = self(arg); } } } Function.prototype.cached7 = function(){ "use strict"; var self = this, cache7 = {}; return function(arg){ if(cache7[arg] !== undefined) { //console.log('Cache hit for '+arg); return cache7[arg]; } else { //console.log('Cache miss for '+arg); return cache7[arg] = self(arg); } }; } //memo1 //underscore.js implem function memoize1(func, hasher) { var memo = {}; hasher || (hasher = function(value){ return value; //^this could be done better..surely.. }); return function() { var key = hasher.apply(this, arguments); return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments)); }; }; //memo2 - another variation with changes by @addyosmani //based on initial work by thejit //note: I'm aware of the perils of hanging the implem off //fn, just want to find out if this version performs much better function memoize2(fn) { return function () { var args = Array.prototype.slice.call(arguments), hash = "", i = args.length; while(i--){ hash += (Object.prototype.toString.call({}) == Object.prototype.toString.call(args[i])) ? JSON.stringify(args[i]) : args[i]; fn.memoize = fn.memoize || {}; } return (hash in fn.memoize) ? fn.memoize[hash] : fn.memoize[hash] = fn.apply(this, args); }; } //memo3 - unscriptables implem. function memoize3(func, context) { function memoizeArg (argPos) { var cache = {}; return function () { 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)) { cache[arguments[argPos]] = memoizeArg(argPos - 1); } return cache[arguments[argPos]].apply(this, arguments); } } } // JScript doesn't grok the arity property, but uses length instead var arity = func.arity || func.length; return memoizeArg(arity - 1); } //memo4 - by stevenlevithan function memoize4(functor, expiration) { var memo = {}; return function () { var key = Array.prototype.join.call(arguments, "§"); if (key in memo) return memo[key]; if (expiration) setTimeout(function () {delete memo[key];}, expiration); return memo[key] = functor.apply(this, arguments); }; } //memo5 - @gbradley function memoize5(fn){ var lookup={}; return function(){ var args=[].slice.call(arguments); var key=JSON.stringify(args); var result=lookup[key]; if (!result){ result=fn.apply(this,args); lookup[key]=result; } return result; }; } function memoize7( fn ) { return function () { var args = Array.prototype.slice.call(arguments), hash = "", i = args.length, hasHash = false; while(i){ hash += (Object.prototype.toString.call({}) == Object.prototype.toString.call(args[--i])) ? JSON.stringify(args[i]) : args[i]; fn.memoize = fn.memoize || {}; } hasHash = fn.memoize[hash] !== undefined; return (hasHash) ? fn.memoize[hash] : fn.memoize[hash] = fn.apply(this, args); }; } function fib(x) { if(x < 2) return 1; else return fib(x-1) + fib(x-2); } // Redefine to prevent any unknown special inlines... function fib7(x) { if(x<2) return 1; else return fib7(x-1) + fib7(x-2); } var fibTest1 = memoize1(fib); var fibTest2 = memoize2(fib); var fibTest3 = memoize3(fib); var fibTest4 = memoize4(fib,1000); var fibTest5 = memoize5(fib); var fibTest6 = fib.cached(); var fibTest7 = memoize7(fib); var fibTest7c = fib7.cached7(); </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code fibTest1(20); fibTest1(20); Test 2 Title Async (check if this is an asynchronous test) Code fibTest2(20); fibTest2(20); Test 3 Title Async (check if this is an asynchronous test) Code fibTest3(20); fibTest3(20); Test 4 Title Async (check if this is an asynchronous test) Code fibTest4(20); fibTest4(20); Test 5 Title Async (check if this is an asynchronous test) Code fibTest5(20); fibTest5(20); Test 6 Title Async (check if this is an asynchronous test) Code fibTest6(20); fibTest6(20); Test 7 Title Async (check if this is an asynchronous test) Code fibTest7(20); fibTest7(20); Test 8 Title Async (check if this is an asynchronous test) Code fibTest7c(20); fibTest7c(20);