Script Preparation code:
x
 
var str = 'abcdef';
str = str.charAt(Math.floor(Math.random() * 6));
function fn1() {return 'one'};
function fn2() {return 'two'};
function fn3() {return 'three'};
function switchFn(s) {
  switch (s) {
    case 'a': return fn1();
    case 'b': return fn2();
    case 'c': return fn3();
    case 'd': return fn1();
    case 'e': return fn2();
    case 'f': return fn3();
  }
}
Tests:
  • Switch

     
    switchFn(str);
  • Object Literal

     
    var objLiteral = {
      a: fn1,
      b: fn2,
      c: fn3,
      d: fn1,
      e: fn2,
      f: fn3
    };
    objLiteral[str]();
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Chrome 104 on Windows
View result in a separate tab
Test name Executions per second
Switch 7063025.0 Ops/sec
Object Literal 3080671.5 Ops/sec