Editing If-Else Chain VS Switch(true) 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) This test is to attempt to determine if there is any notable performance difference when using switch(true) instead of a chain of if-else statements. 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 strs = ['apple','banana','pear','lemon','grape','orange'], len = strs.length; </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 for(var i = len; i--;) { var str = strs[i]; if(/p{2}le/.test(str)) var some_var = 'found apple'; else if(/(an){2}a/.test(str)) var some_var = 'found banana'; else if(/^[a-z][a-z][a-z][a-z]$/.test(str)) var some_var = 'found pear'; else if(str.indexOf("lemon") != -1) var some_var = 'found lemon'; else if(/ape/.test(str)) var some_var = 'found grape'; else var some_var = 'found else (orange)'; } Test 2 Title Async (check if this is an asynchronous test) Code for(var i = len; i--;) { var str = strs[i]; switch(true) { case /p{2}le/.test(str): var some_var = 'found apple'; break; case /(an){2}a/.test(str): var some_var = 'found banana'; break; case /^[a-z][a-z][a-z][a-z]$/.test(str): var some_var = 'found pear'; break; case (str.indexOf("lemon") != -1): var some_var = 'found lemon'; break; case /ape/.test(str): var some_var = 'found grape'; break; default: var some_var = 'found else (orange)'; } } Test 3 Title Async (check if this is an asynchronous test) Code var ans = [ 'found else (orange)', 'found apple', 'found banana', 'found pear', 'found lemon', 'found grape' ]; for(var i = len; i--;) { var str = strs[i]; var some_var = ans[+(/p{2}le/.test(str) || /(an){2}a/.test(str) * 2 || /[a-z][a-z][a-z][a-z]/.test(str) * 3 || str.indexOf("lemon") * 4 || /ape/.test(str) * 5)]; }