Conditionals (v5)

Revision 5 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script>
<script>
 var a = 'test',
     x;
 
 function isValueInArray(arr, val) {
  inArray = false;
  for (i = 0; i < arr.length; i++) {
   if (val === arr[i]) {
    inArray = true;
   }
  }
  return inArray;
 }
 
 function oc(a) {
  var o = {};
  for (var i = 0; i < a.length; i++) {
   o[a[i]] = '';
  }
  return o;
 }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Or
x = ((a == 'foo') || (a == 'bar') || (a == 'none') || (a == 'test'));
ready
switch / case
switch (a) {
case 'foo':
case 'bar':
case 'none':
case 'test':
 x = true;
 break;
default:
 x = false;
}
ready
In Object
x = a in {
 'foo': 1,
 'bar': 1,
 'none': 1,
 'test': 1
};
ready
isValueInArray
//http://snipplr.com/view/12299/is-value-in-array
x = isValueInArray(['foo', 'bar', 'none', 'test'], a);
ready
Object Converter / In
//http://snook.ca/archives/javascript/testing_for_a_v
x = a in oc(['foo', 'bar', 'none', 'test']);
ready
Regex
//http://snook.ca/archives/javascript/testing_for_a_v#c24260
x = /^(?:foo|bar|none|test)$/.test(a)
ready
Ternary
x = (a == 'foo') ? 1 : (a == 'bar') ? 1 : (a == 'none') ? 1 : (a == 'test') ? 1 : 0;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.