Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use haml or erb or erubis for potentially high traffic site?

Haml rocks. I haven't seen any recent performance numbers but it is pretty close to erb these days. I think that it might be faster than erb if you turn on ugly mode (which prevents the pretty indentation) We're doing 2.8 million pageviews a day with Haml.

There is a benchmarker checked into the Haml source tree: http://github.com/nex3/haml/tree/master/test

Update November 2009

Nathan (Haml's main developer) published some Haml 2.2 benchmarks on his blog. You can see the exact numbers there but in short:

  • Normal (pretty printing) mode = 2.8 times slower than ERB
  • Ugly mode (no pretty tabs added) = equal to ERB

You can enable ugly mode by placing Haml::Template::options[:ugly] = true in an initializer or environment file. Note that ugly mode isn't really that ugly - the resulting HTML is actually much prettier than ERB - it's just not indented nicely.


If you use Rails, the performance difference between Haml and erubis is negligible: templates get compiled and cached after the first hit, anyway. Combine this with fragment and page caching and you can rest assured that views are not the performance bottleneck of your application.

The question you should be asking yourself is: do you like writing Haml? Does it make you more productive? Then you can decide easier.


I love HAML since it is a good tool for easily writing structured HTML, and generally it is just a joy to use. But it has very little to do with choosing a tool based on the amount of traffic a site might have.

If you are worried about traffic, you should worry about using caching properly. And then you need to apply the principles of general web-application performance - the result is that you will have super snappy responses to page loads. Which is what a high-traffic website really needs.

A couple of presentations that show how to improve website performance can be found here:

  • Michael Koziarski speaks about Rails Performance on 2008 Paris on Rails

  • Jeremy Kemper talks about Performance on Rails on RailsConf EU'08

And the best place that I know of to learn how to use rails caching properly is:

  • Rails Caching PeepCode screencast

I think it's entirely a matter of personal preference and maintainability. For me, Haml makes the templates easier to read and understand, and the performance is very acceptable. In the end, the templating language is unlikely to be the place where you need to optimize -- more likely database queries, view or object caching, etc.

However, in the case of ERb templates, you will get better performance essentially for free if you use erubis.