Editing Duff's device 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) Simple test of variations of [loop unrolling/unwinding](http://en.wikipedia.org/wiki/Loop_unwinding) based on the original [Duff's Device](http://en.wikipedia.org/wiki/Duff%27s_device). Result may vary based on the number of iterations, browser, OS and external influences... Usually *Fast Duff's Device #2, #3, #4* runs faster when the number of iterations is bigger or when the test is executed multiple times without browser refresh - bitwise operations have a really strange behavior in JS, sometimes they are really fast and other times they run slower than the `Math` methods. Make sure you test each approach using real application code (since results may vary) and just use this kind of optimization [if you really need it](http://jsperf.com/duffs-device)! Try to combine techniques and avoid property lookups! *PS: It's over 9000 iterations!* 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> var iterations = 1000000; </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 var testVal = 0; var n = iterations; while (n--) { testVal++; } Test 2 Title Async (check if this is an asynchronous test) Code /** * Duff's Device * http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#duffsdevice */ var testVal = 0; var n = iterations / 8; var caseTest = iterations % 8; do { switch (caseTest) { case 0: testVal++; case 7: testVal++; case 6: testVal++; case 5: testVal++; case 4: testVal++; case 3: testVal++; case 2: testVal++; case 1: testVal++; } caseTest = 0; } while (--n > 0); Test 3 Title Async (check if this is an asynchronous test) Code /** * Fast Duff's Device * @author Jeff Greenberg * http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#fastDuffsdevice */ var testVal = 0; var n = iterations % 8; while (n--) { testVal++; } n = parseInt(iterations / 8); while (n--) { testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; } Test 4 Title Async (check if this is an asynchronous test) Code /* * Fast Duff's Device * @author Miller Medeiros <http://millermedeiros.com> * @version 0.1 (2010/08/25) */ var testVal = 0; var n = iterations % 8; while (n--) { testVal++; } n = Math.floor(iterations / 8); while (n--) { testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; } Test 5 Title Async (check if this is an asynchronous test) Code /* * Fast Duff's Device * @author Miller Medeiros <http://millermedeiros.com> * @version 0.2 (2010/08/25) */ var testVal = 0; var n = iterations % 8; while (n--) { testVal++; } n = (iterations / 8) ^ 0; //`value ^ 0` is the same as `Math.floor` for positive numbers and `Math.ceil` for negative numbers while (n--) { testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; } Test 6 Title Async (check if this is an asynchronous test) Code /* * Fast Duff's Device * @author Miller Medeiros <http://millermedeiros.com> * @version 0.3 (2010/08/25) */ var testVal = 0; var n = iterations % 8; while (n--) { testVal++; } n = (iterations * 0.125) ^ 0; //multiplication is faster than division in some cases while (n--) { testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; testVal++; }