Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0
Firefox 104
Mac OS X 10.15
Desktop
2 years ago
Test name Executions per second
Switch 312253.9 Ops/sec
Object Literal 280195.8 Ops/sec
If else 270907.0 Ops/sec
Script Preparation code:
AخA
 
var str = 'abc';
str = str.charAt(Math.floor(Math.random() * 3));
Tests:
  • Switch

     
    switch (str) {
      case 'a': console.log('A'); break;
      case 'b': console.log('B'); break;
      case 'c': console.log('C'); break;
    }
  • Object Literal

    x
     
    var objLiteral = {
      a: function() {
        console.log('A');
      },
      b: function() {
        console.log('B');
      },
      c: function() {
        console.log('C');
      }
    }
    objLiteral[str]();
  • If else

     
    if(str === 'a'){
        console.log('a')
    }
    if(str === 'b'){
        console.log('b')
    }
    if(str === 'c'){
        console.log('c')
    }