Skip to content

Comment on Show HN: multimethod.js - a Clojure-inspired multimethod library for JavaScript

Comments

Nice stuff, I liked the approach.

But what is the benchmark for this kind of structure against regular JS workflow? I guess it will be slower, but how much ?

Good question.

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.

It doesn't have to be any slower than highly optimized switch statements if you're willing to compile to JavaScript. All you need analysis / compilation support. I've been working on this for a while now for Clojure / ClojureScript.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.