Editing sssssssssssssss 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) 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 defer = (function(){ //wrapped in IIFE to provide a scope for deferreds and wrap var running = false; var deferreds = [function(){alert(1)},function(){alert(2)}]; function wrap(func){ return function(){ func(); var next = deferreds.shift(); if(next){ setTimeout(wrap(next),0); }else{ running = false; } } } return function(func){ if(running){ deferreds.push(func); }else{ setTimeout(wrap(func),0); running = true; } } })() Test 2 Title Async (check if this is an asynchronous test) Code function inOrderTimeout(/* func1, [func2, func3, ...funcN,] timeout */) { var id, args = arguments, numToRun = args.length - 1; /*if (numToRun < 1) { throw new Error('You did not provide at least 1 function to run for inOrderTimeout().\nCorrect syntax is as follows:\n\ninOrderTimeout(func1, [func2, func3, ...funcN] timeout)'); return; }*/ var currentFunc = 0, timeout = args[numToRun]; //array index is the same as the number of functions to run /*if (typeof timeout !== 'number' && timeout !== '0' && +timeout === 0 || timeout === Infinity || timeout === -Infinity || +timeout !== +timeout) { throw new Error('You did not provide a valid number, or it was equal to positive or negative Infinity. Only integers and string representations of integers (without anything else in it) are permitted.'); return; }*/ (function caller(func, timeout) { if (currentFunc > numToRun - 1) { clearTimeout(id); return; } id = setTimeout(function () { func(); caller(args[++currentFunc], timeout); }, Math.floor(timeout)); }(args[currentFunc], timeout)); } inOrderTimeout(function(){alert(1)},function(){alert(2)},0)