Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working on a project and I am still thinking about using HAML (beautiful code, less size of view files) instead of classic ERB template.

My worries why I didn't do it yet are the speed of generating views - I read an articles/benchmarks and there was almost always HAML slower than ERB - but the truth is, that the articles are 2-3 years old.

So my question is, how looks the comparison these two template systems now, in the start of 2012?

like image 645
user984621 Avatar asked Feb 12 '12 02:02

user984621


People also ask

Is Haml a good choice for web development with Ruby on rails?

Results: Clearly, HAML is a modern and popular choice for web development with Ruby on Rails 6. P.S. VERY useful websites that help converting bulk code to/from haml, that I use on a daily basis:

Why do you use Haml?

I’m often asked why I use HAML for my projects. wide adoption: some time ago I did a poll on r/rails to see it’s pupularity. Results: Clearly, HAML is a modern and popular choice for web development with Ruby on Rails 6.

What is the difference between Erb and Slim?

The focus is on assigning the template and rendering it. The two result blocks are separated for you to assimilate what’s real from the rehearsal. For the last test results, you could assume that ERB is the best option, while SLIM is the worst.


2 Answers

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.

like image 50
siannopollo Avatar answered Oct 04 '22 16:10

siannopollo


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.

like image 36
Ashish Sharma Avatar answered Oct 04 '22 17:10

Ashish Sharma