Tests:
  • Vanilla Switch

    x
     
    function isFunc (v) {
      return typeof v === 'function';
    }
    function isDefined (v)  {
      return v !== undefined && v !== null;
    }
    function switchEnum(e, handlers) {
      const specific = handlers[e];
      if (isDefined(specific)) {
        return isFunc(specific) ? specific(e) : specific;
      }
      if (isDefined(handlers.else)) {
        return isFunc(handlers.else) ? handlers.else(e) : handlers.else;
      }
      throw new Error(`Unhandled switchEnum statement for value (${e}).`);
    }
    function normal(inp) {
      switch (inp) {
        case "today":
          return 1;
        case "tomorrow":
          return 2;
        case "yesterday":
          return -1;
        default:
          return 0; 
      }
    };
    normal('today');
    normal('yesterday');
    normal('tomorrow');
  • Switch Fn style

     
    function isFunc (v) {
      return typeof v === 'function';
    }
    function isDefined (v)  {
      return v !== undefined && v !== null;
    }
    function switchEnum(e, handlers) {
      const specific = handlers[e];
      if (isDefined(specific)) {
        return isFunc(specific) ? specific(e) : specific;
      }
      if (isDefined(handlers.else)) {
        return isFunc(handlers.else) ? handlers.else(e) : handlers.else;
      }
      throw new Error(`Unhandled switchEnum statement for value (${e}).`);
    }
    function funcccy(inp) {
     return switchEnum(inp, {
       "today": () => 1,
       "tomorrow": () => 2,
       "yesterday": () => -1,
       "else": () => 0,
      });
    }
    funcccy('today');
    funcccy('yesterday');
    funcccy('tomorrow');
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Vanilla Switch
    Switch Fn style

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0
Firefox 125 on Windows
View result in a separate tab
Test name Executions per second
Vanilla Switch 63907816.0 Ops/sec
Switch Fn style 8259009.5 Ops/sec