if-vs-switch-statement2

JavaScript performance comparison

Test case created by SanSYS

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
   function getCategoryByIf(age) {  
          if (isNaN(age))
              return "not an age";  
  
          if (age >= 44)  
              return "44 Old";  
  
          if (age >= 45)  
              return "45 Old";  
  
          if (age === 33)  
              return "33 Old";  
  
          if (age >= 42)  
              return "42 Old";  
          
          if (age >= 50)  
              return "Old";  
          
          if (age <= 20)  
              return "Baby";  
          
          return "Young";  
      }
      
      function getCategoryByIfElse(age) {  
          if (isNaN(age))
              return "not an age";
          else  
  
          if (age >= 44)  
              return "44 Old";  
  
          else
  
          if (age >= 45)  
              return "45 Old";  
          else
  
          if (age === 33)  
              return "33 Old";  
          else
  
          if (age >= 42)  
              return "42 Old";  
          else
          
          if (age >= 50)  
              return "Old";  
          else
          
          if (age <= 20)  
              return "Baby";  
          else
          
          return "Young";  
      }
      
      function getCategoryBySwitch(age) {  
          switch (true) {  
              case isNaN(age):  
                  return "not an age";  
      
              case (age === 45):  
                  return "45 Old";  
  
              case (age === 44):  
                  return "44 Old";  
  
              case (age === 33):  
                  return "33 Old";  
  
              case (age === 42):  
                  return "42 Old";  
  
              case (age >= 50):  
                  return "Old";  
        
              case (age <= 20):  
                  return "Baby";  
        
              default:  
                  return "Young";  
          };  
      }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
getCategoryByIf()
getCategoryByIf()
pending…
getCategoryByIfElse()
getCategoryByIfElse()
pending…
getCategoryBySwitch()
getCategoryBySwitch()
pending…
getCategoryByIf(5)
getCategoryByIf(5)
pending…
getCategoryByIfElse(5)
getCategoryByIfElse(5)
pending…
getCategoryBySwitch(5)
getCategoryBySwitch(5)
pending…
getCategoryByIf(50)
getCategoryByIf(50)
pending…
getCategoryByIfElse(50)
getCategoryByIfElse(50)
pending…
getCategoryBySwitch(50)
getCategoryBySwitch(50)
pending…
getCategoryByIf(35)
getCategoryByIf(35)
pending…
getCategoryByIfElse(35)
getCategoryByIfElse(35)
pending…
getCategoryBySwitch(35)
getCategoryBySwitch(35)
pending…

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

Compare results of other browsers

0 Comments