Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36
Chrome 103
Windows
Desktop
2 years ago
Test name Executions per second
Switch 304891.4 Ops/sec
Object Literal 282725.0 Ops/sec
If else 283235.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')
    }