If statements

JavaScript performance comparison

Revision 12 of this test case created by Muhammad Umer

Info

which one is faster from among ways at http://stackoverflow.com/questions/18347033/how-to-shorten-my-conditional-statements/18347354#18347354

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var ct = 'itemz';
   
   
    var possibilities = {
         'itema':true,
         'itemb':true,
         'itemc':true,
         'itemd':true,
         'iteme':true,
         'itemf':true,
         'itemg':true,
         'itemh':true,
         'itemi':true,
         'itemj':true,
         'itemk':true,
         'iteml':true,
         'itemm':true,
         'itemn':true,
         'itemo':true,
         'itemp':true,
         'itemq':true,
         'itemr':true,
         'items':true,
         'itemt':true,
         'itemu':true,
         'itemv':true,
         'itemw':true,
         'itemx':true,
         'itemy':true,
         'itemz':true
    };
    var arr = ['itema','itemb','itemc','itemd','iteme','itemf','itemg','itemh','itemi','itemj','itemk','iteml','itemm','itemn','itemo','itemp','itemq','itemr','items','itemt','itemu','itemv','itemw','itemx','itemy','itemz'];
   
    function oc(a)
    {
      var o = {};
      for(var i=0;i<a.length;i++)  o[a[i]]='';
      return o;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
switch-case
switch (ct) {
  case "itema":
  case "itemb":
  case "itemc":
  case "itemd":
  case "iteme":
  case "itemf":
  case "itemg":
  case "itemh":
  case "itemi":
  case "itemj":
  case "itemk":
  case "iteml":
  case "itemm":
  case "itemn":
  case "itemo":
  case "itemp":
  case "itemq":
  case "itemr":
  case "items":
  case "itemt":
  case "itemu":
  case "itemv":
  case "itemw":
  case "itemx":
  case "itemy":
  case "itemz":
  var x='123'+123;
}
pending…
~ and indexOf
if ( ~arr.indexOf(ct) ) {
    var x='123'+123;
}
pending…
using | |
if (ct == 'itema' || ct == 'itemb' ||  ct == 'itemc' || ct == 'itemd' || ct == 'iteme' || ct == 'itemf' || ct == 'itemg' || ct == 'itemh' || ct == 'itemi' || ct == 'itemj' || ct == 'itemk' || ct == 'iteml' || ct == 'itemm' || ct == 'itemn' || ct == 'itemo' || ct == 'itemp' || ct == 'itemq' || ct == 'itemr' || ct == 'items' || ct == 'itemt' || ct == 'itemu' || ct == 'itemv' || ct == 'itemw' || ct == 'itemx' || ct == 'itemy' || ct == 'itemz') {
    var x='123'+123;
}
pending…
using 'in' and Object
if (ct in possibilities) { 
    var x='123'+123;
}
pending…
regex
if( /^item[a-z]$/.test(ct) ) {
  var x='123'+123;
}
pending…
Using 'in' and Function
if (ct in oc(arr)) { 
  var x='123'+123;
}

 
pending…
object lookup
if (possibilities[ct]) { 
    var x='123'+123;
}
pending…
index of string
if ("/itema/itemb/itemc/itemd/iteme/itemf/itemg/itemh/itemi/itemj/itemk/iteml/itemm/itemn/itemo/itemp/itemq/itemr/items/itemt/itemu/itemv/itemw/itemx/itemy/itemz".indexOf(ct)>0) {
var x='123'+123;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Muhammad Umer (revision owner) commented :

Who knew object look up could compete...

Add a comment