Rails 3.2 - haml vs. erb. Is haml faster? (february 2012)

There are already a bunch of debates documented on the web.

But when We have an application where we used ERB exclusively. Why should we try to make the switch to HAML? To me, it's a waste of time and a poor decision that wouldn't provide many benefits. While our code would suddenly be cleaner and a little more beautiful in HAML, we'd waste precious hours converting ERB to HAML. We have so many other important things to do, why waste that time on something so pointless in this case?

It's all relative. You don't need to use HAML instead of ERB. ERB can do all the same things. They are just different tools for the same job.


Here are some benchmarks from someone in Nov 2011. You should be able to cd to your haml directory and run rake benchmark. I say "should" because you'll need to get the proper dependencies installed in order to run that task. I played around with it for a few minutes but no luck locally. The relevant file is haml/test/benchmark.rb.

EDIT: I did find some more information for you. Someone else had the same trouble as me trying to get benchmarks working locally, so they rolled their own. I forked that gist and added support for comparing haml to erb: https://gist.github.com/1807036. I used the same templates that the haml library uses to benchmark. Here are the results that I got running the code:

$ ruby benchmark.rb
       user     system      total        real
haml:  0.650000   0.000000   0.650000 (  0.651584)
erb:  0.540000   0.000000   0.540000 (  0.534727)

I used ruby 1.9.3-p0, haml 3.1.4.

UPDATE 5/2020: @spickermann on github pointed out a flaw in the benchmark, in that the comparison of haml to erb was not fair. The haml engine instance was only instantiated once, whereas the erb instance was instantiated on each of the 1000 benchmark runs. When we fix the benchmark so we instantiate each instance on each benchmark run we get very different results:

$ ruby benchmark.rb 
       user     system      total        real
haml:  3.259634   0.007936   3.267570 (  3.271728)
erb:  0.205065   0.000571   0.205636 (  0.205824)

This new benchmark uses ruby 2.7.1, haml 5.1.2. The gist has also been updated.