Is Haskell the Cure?

Ted Dziuba's well executed trolling, Node.js is Cancer has been sucessful in getting people all riled up, benchmarking fibonacci servers in all the popular scripting languages.

Both Joshua Kehn and Brian Beck reaches the conclusion that Node is more than fast enough through their benchmarks of Ruby, PHP and Python versions of the Node fibonacci example.

My goto tool for writing fast, concurrent web services is Haskell. So I thought I would see how a direct translation of the fibonacci service would fare with the Snap Framework.

Here's the implementation in 11 lines of Haskell:

So how does it fare?

Well, turns out Haskell is just too clever for this benchmark. Since we're always returning the same number, Haskell ends up just memoizing the result. On my MacBook Pro the time ends up looking like this:

Is Meassuring Fibonacci Relevant?

While benchmarking fibonacci is fun, it might not be terribly relevant when evaluating solutions to concurrent programming.

I think a lot of people missed the main point of Dziuba's troll. Behind the provocation there is quite a bit of truth. Node is a one-trick-pony when it comes to concurrency. You get an event loop and that's it. If you ever need something else, too bad.

From a language point of view node brings nothing to the table. Especially not when compared to Clojure, Erlang, Go or Haskell.

Javascript is all about mutable data types, there's no continuations, no STM, no CSP, no actors, no light-weight threads and no persistent data structures.

Haskell is incredible in this regard. GHC does non-blocking IO out of the box. You get light-weight threads that automatically spreads over multiple cores. Channels, continuations, actors, STM and persistent data structures are all there. The type system does a lot of handholding and make reasoning about concurrent code far easier. There's great support for parallel computations and the language itself is a lot faster than any of the scripting languages.

It comes with a price, though: a humongous learning curve. Going from PHP, Ruby or Python and into the world of Haskell is not trivial. The whole approach to building programs with Haskell is very different from any of these and there's a lot of fairly complex abstractions to learn (Monads, Enumerators, type families, etc). There really is a high barrier to entry.

Node, on the other hand, is extremely straight-forward to pick up. If you've done any AJAX the callback style will be familiar, javascript is a simple language and the approach to programming is no different from Ruby, Python or PHP.

The magic of Node is that it allows an average PHP programmer to do stuff that requires concurrency. That's it, and it's quite an achievement. It's nowhere near the power of Haskell or Clojure, but it's so simple and so familiar that it allows a lot of programmers to achieve something they would otherwise be incapable of doing in their language of choice.

For better or worse, Node is like the PHP of concurrency and ever so often worse is better.

comments powered by Disqus