Script Preparation code:
AخA
 
var str = 'abc';
str = str.charAt(Math.floor(Math.random() * 3));
Tests:
  • Switch

    x
     
    const getString = (randomString) => {
      switch (randomString) {
        case 'a':
          return 'a';
        case 'b':
          return 'b';
        case 'c':
          return 'c';
        case 'd':
          return 'd';
        case 'e':
          return 'e';
        case 'f':
          return 'f';
        case 'g':
          return 'g';
        case 'h':
          return 'h';
        case 'i':
          return 'i';
        case 'j':
          return 'j';
        default: {
          return 'default';
        }
      }
    };
    console.log(getString(str));
  • Object Literal

     
    const objectLiteral = {
      a: 'a',
      b: 'b',
      c: 'c',
      d: 'd',
      e: 'e',
      f: 'f',
      g: 'g',
      h: 'h',
      i: 'i',
      j: 'j',
    };
    console.log(objectLiteral[str] || 'default');
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Switch
    Object Literal

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Chrome 105 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
Switch 455535.8 Ops/sec
Object Literal 451851.8 Ops/sec