Editing Memoization vs. Direct calling 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> /* * memoize.js * by @philogb and @addyosmani * further optimizations by @mathias * Released under an MIT license. */ function memoize2( 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 = {}); } return (hash in fn.memoize) ? fn.memoize[hash] : fn.memoize[hash] = fn.apply( this , args ); }; } //stoyans version function memoize7(param){ if (!memoize7.cache) { memoize7.cache = {}; } if (!memoize7.cache[param]) { var result = fib(param); memoize7.cache[param] = result; } return memoize7.cache[param]; } var fib, fiborg, f; fiborg = fib = f = function (x) { if(x < 2) return 1; else return fib(x-1) + fib(x-2); } </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 fib = memoize2(fiborg); console.log(fib(25),'test1-norm'); Test 2 Title Async (check if this is an asynchronous test) Code console.log(fib(25)); console.log(fib(25)); //on purpose as a second call is being made. This is to simulate the cache request found in the other tests. Test 3 Title Async (check if this is an asynchronous test) Code console.log(memoize7(25),'test7-norm');