« Rails 1.2 doesn’t support composite primary keys | Home | Faceted search in Solr with Ruby on Rails »

Haml and Rails rendering speed

By Nick | July 13, 2007

I was shocked when I read that the new 1.7 release of Haml now comes with speed. Haml 1.7 is now only 1.8 times slower than ERB. And I thought ERB was one of the slowest things in Rails. Check out http://www.kuwata-lab.com/erubis/ for different rendering speeds and notice that ERB is the slowest of them all. So saying that Haml now comes with speed is a nice euphemism. If you need more rendering speed I advice you to use Erubis (http://www.kuwata-lab.com/erubis/). The only thing you need to do to install it is:

gem install erubis

and put - require ‘erubis/helpers/rails_helper’ - in your environment.rb file.

Topics: Uncategorized |

2 Responses to “Haml and Rails rendering speed”

  1. Hampton Says:
    July 16th, 2007 at 7:03 pm

    You have to remember though that for *most* applications the actual rendering time of the page itself (ignore what rails reports as "rendering" time… its a lie) is in milliseconds.

    So sure, Erubis is super-fast. I would do it if I was writting the twitter server or something and ultra-fast responses were paramount and milliseconds matter.

    However, I’ve found that in general my application speed problems are not due to my templates. They are due to database, helpers, and all the other shit going on in your Rails app.

    Its not 1.8 times slower, by the way. Its 1.3-4 times slower on my machine. Moreover, we are far from done with the speed increases.

  2. Tom Bagby Says:
    July 18th, 2007 at 3:15 am

    It “now comes with speed” in the sense that before it was substantially slower. It has come a long way. The difference with Haml is that a lot of extra formatting happens to keep the HTML output looking clean which definitely has a performance penalty. Much of the new optimization is directly inspired by how erubis does template compilation.

    Even 1.7 was just released, the development version of Haml has already improved down to 1.2x ERB. With an option to switch off formatting that will soon be available, it beats ERB, though still some work to get it as fast as erubis.

    However, take all benchmarks with a grain of salt. On very static templates, the difference between all of them is much less (or none at all). If every line is a script output, Haml gets relatively slower because it is doing more formatting work. If many tag attributes are individually set by script, Haml’s relative performance is much better because that’s clunky to do in ERB/erubis.

    I personally use both Haml and erubis. If it’s not fast enough for you yet, wait for 2.0 I guess! At least Haml is now in the same ballpark as RHTML, unlike for instance Markaby which is a good 20x slower.

Comments