But what is the benchmark for this kind of structure against regular JS workflow? I guess it will be slower, but how much ?
Performance will depend on a number of factors, such as how complex your dispatch function is, how complex your dispatched values and match values are (deep equality becomes more expensive as the compared objects grow in complexity), and how many methods are registered. In general, it is much more expensive than native switch statements or prototype chain lookups.
If performance is of utmost importance and you can substitute a switch statement or prototype hierarchy for a multimethod, you should. Multimethods are a useful pattern for achieving things that either aren't possible in switch (add/remove cases dynamically, deep equality case matching) or are not naturally/painlessly expressed with a prototype hierarchy.
fib = (n) -> switch n
when 0 then 0
when 1 then 1
else fib(n-1) + fib(n-2)
alert fib 20To illustrate this flexibility in the context of fib, multimethod.js, and CoffeeScript:
> fib = multimethod()
.when(0, 0)
.when(1, 1)
.default (n) -> fib(n)
> fib 20
6765
> fib.dispatch (n) -> n.numberVal or n
> fib 20
6765
> fib { numberVal: 20 }
6765
> fib.when 2, 1
> fib.when 3, 2
> fib 20
6765 (now "optimized" with fewer recursive calls than before)