Run details:
Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 [FBAN/FBIOS;FBDV/iPhone12,1;FBMD/iPhone;FBSN/iOS;FBSV/13.3;FBSS/2;FBID/phone;FBLC/pt_BR;FBOP/5;FBCR/TIM]
Mobile Safari 13
iOS 13.3
Mobile
5 years ago
Test name Executions per second
Switch 1173011.6 Ops/sec
Object Function 1108220.2 Ops/sec
Object Property 1154710.0 Ops/sec
Script Preparation code:
AخA
 
var str = 'abc';
str = str.charAt(Math.floor(Math.random() * 3));
Tests:
  • Switch

    x
     
    function test (str) {
      switch (str) {
        case 'a': return 'A'; break;
        case 'b': return 'B'; break;
        case 'c': return 'C'; break;
      }
    }
    console.log(test(str));
  • Object Function

     
    const obj = {
      a: () => 'A',
      b: () => 'B',
      c: () => 'C'
    };
    console.log(obj[str]());
  • Object Property

     
    const obj = {
      a: 'A',
      b: 'B',
      c: 'C'
    };
    console.log(obj[str]);