obj vs case

JavaScript performance comparison

Test case created by viebel

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var myObj = {0: "A", 1: "B", 2: "C", 3: "D", 4: "E", 5: "F", 6: "G", 7: "H", 8: "I", 9: "J", 10: "K", 11: "L", 12: "M", 13: "N", 14: "O", 15: "P", 16: "Q", 17: "R", 18: "S", 19: "T", 20: "U", 21: "V", 22: "W", 23: "X", 24: "Y", 25: "Z", 26: "a", 27: "b", 28: "c", 29: "d", 30: "e", 31: "f", 32: "g", 33: "h", 34: "i", 35: "j", 36: "k", 37: "l", 38: "m", 39: "n", 40: "o", 41: "p", 42: "q", 43: "r", 44: "s", 45: "t", 46: "u", 47: "v", 48: "w", 49: "x", 50: "y", 51: "z", 52: "0", 53: "1", 54: "2", 55: "3", 56: "4", 57: "5", 58: "6", 59: "7", 60: "8", 61: "9", 62: "+", 63: "/"};
  
  
  function retrieveFromObj (n) {
    return myObj[n];
  };
  
  function retrieveFromObj2 (n) {
    var e =  myObj[n];
    if(e !== null) {
      return e;
    } else {
      throw "bad"
    }
  };
  
  function switchCase (a) {
    switch(a) {
      case 0:
        return "A";
      case 1:
        return "B";
      case 2:
        return "C";
      case 3:
        return "D";
      case 4:
        return "E";
      case 5:
        return "F";
      case 6:
        return "G";
      case 7:
        return "H";
      case 8:
        return "I";
      case 9:
        return "J";
      case 10:
        return "K";
      case 11:
        return "L";
      case 12:
        return "M";
      case 13:
        return "N";
      case 14:
        return "O";
      case 15:
        return "P";
      case 16:
        return "Q";
      case 17:
        return "R";
      case 18:
        return "S";
      case 19:
        return "T";
      case 20:
        return "U";
      case 21:
        return "V";
      case 22:
        return "W";
      case 23:
        return "X";
      case 24:
        return "Y";
      case 25:
        return "Z";
      case 26:
        return "a";
      case 27:
        return "b";
      case 28:
        return "c";
      case 29:
        return "d";
      case 30:
        return "e";
      case 31:
        return "f";
      case 32:
        return "g";
      case 33:
        return "h";
      case 34:
        return "i";
      case 35:
        return "j";
      case 36:
        return "k";
      case 37:
        return "l";
      case 38:
        return "m";
      case 39:
        return "n";
      case 40:
        return "o";
      case 41:
        return "p";
      case 42:
        return "q";
      case 43:
        return "r";
      case 44:
        return "s";
      case 45:
        return "t";
      case 46:
        return "u";
      case 47:
        return "v";
      case 48:
        return "w";
      case 49:
        return "x";
      case 50:
        return "y";
      case 51:
        return "z";
      case 52:
        return "0";
      case 53:
        return "1";
      case 54:
        return "2";
      case 55:
        return "3";
      case 56:
        return "4";
      case 57:
        return "5";
      case 58:
        return "6";
      case 59:
        return "7";
      case 60:
        return "8";
      case 61:
        return "9";
      case 62:
        return "+";
      case 63:
        return "/";
      default:
        throw "bad";
    }
  };

};
</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
retrieveFromObj
retrieveFromObj(63)
pending…
switchCase
switchCase(63)
pending…
retrieveFromObj2
retrieveFromObj2(63)
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