Switch vs. hash (v11)

Revision 11 of this benchmark created on


Preparation HTML

<script>
  function getShortMonth(month) {
   switch (month) {
   case 0:
    return 'Jan';
   case 1:
    return 'Feb';
   case 2:
    return 'Mar';
   case 3:
    return 'Apr';
   case 4:
    return 'May';
   case 5:
    return 'Jun';
   case 6:
    return 'Jul';
   case 7:
    return 'Aug';
   case 8:
    return 'Sep';
   case 9:
    return 'Oct';
   case 10:
    return 'Nov';
   case 11:
    return 'Dec';
   default:
    return false;
   }
  }
  
  shortMonth = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  
  function getRandomMonthNum() {
   return Math.floor(Math.random() * 12);
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
getShortMonth(getRandomMonthNum());
ready
Hash
shortMonth[getRandomMonthNum()];
ready

Revisions

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