1. 24
    1. 2

      I love this JavaScript pattern. It can also be used with objects and written with a fallback, e.g.:

      const opNamesToFunctions = {
        op1: () => // ...
        op2: () => // ...
        // ...
      }
      
      const noop = () => undefined;
      
      const op = opNamesToFunctions[opName] || noop;
      return op();
      

      It’s kind of a poor person’s pattern matching, but it works great and I find it much easier to read than switch blocks. It works especially well in TypeScript if you can strictly define the keys of the object with a union type. Glad to hear it’s more performant, too.

      1. 3

        Works quite nicely in Perl also (which famously didn’t have switch blocks for decades, and now… kinda does.)

      2. 3

        I use this pattern in clojure to implement processors for advent of code! Clean, consistent, easy to reason about.

    2. 1

      Oof. This must also have also affected parsers a lot, since there’s often a big switch in the lexer and another in the parser.

    3. 1

      I am curious if they ever did fix the optimization issue in v8.