Editing Trig Memoization Degrees 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) 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) function degSin(deg){ return Math.sin(deg*Math.PI/180); } function degCos(deg){ return Math.cos(deg*Math.PI/180); } function degTan(deg){ return Math.tan(deg*Math.PI/180); } function myMemoize(f) { /// My own memoization function. return function (x) { // If the function doesn't have a cache, add one. f.cache = f.cache || {}; // If we've haven't done this before if( ! (x in f.cache) ){ // Cache it f.cache[x] = f(x); } return f.cache[x]; }; } myMemSin = myMemoize(degSin); myMemCos = myMemoize(degCos); myMemTan = myMemoize(degTan); function memo(f) { /// Source: http://philogb.github.com/blog/2008/09/05/memoization-in-javascript/ return function (x) { f.memo = f.memo || {}; return (x in f.memo)? f.memo[x] : f.memo[x] = f(x); }; } memoSin = memoize(degSin); memoCos = memoize(degCos); memoTan = memoize(degTan); /* * memoize.js * by @philogb and @addyosmani * with further optimizations by @mathias * and @DmitryBaranovsk * perf tests: http://bit.ly/q3zpG3 * Released under an MIT license. */ 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); }; } otherSin = memoize(degSin); otherCos = memoize(degCos); otherTan = memoize(degTan); var deg = Math.random()*360; 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 degSin(deg); degCos(deg); degTan(deg); Test 2 Title Async (check if this is an asynchronous test) Code myMemSin(deg); myMemCos(deg); myMemTan(deg); Test 3 Title Async (check if this is an asynchronous test) Code memoSin(deg); memoCos(deg); memoTan(deg); Test 4 Title Async (check if this is an asynchronous test) Code otherSin(deg); otherCos(deg); otherTan(deg);