if statement with many ORs

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var str = "", x=25;
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
if statement
if (x==1 || x==15 || x==25 || x==150){
    str = "ok";
}
pending…
switch-case statement
switch (x) {
case 1:
case 15:
case 25:
case 150:
  str = "ok";
  break;
}
pending…
array.indexOf
if ([1, 15, 25, 150].indexOf(x) > -1) {
    str = "ok";
}
pending…
jQuery.inArray statement
if(jQuery.inArray(x,[1,15,125,150]) > -1) {
    str = "ok";
}
pending…
object properties
var items = {1:true, 15:true, 25:true, 150:true};
if (items[x] === true) {
    str = "ok";
}
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:

0 comments

Add a comment