1

We are looking for fastest template engine for rendering of views.

As i understand erubis is the fastest template engine in ruby.

My usecase is render templates through script.

Looking at the gem official page it's latest release was in 2011. Not sure if the community is active. https://rubygems.org/gems/erubis/versions

Does anyone use ruby 2.1 with erubis template engine?

Is it recommended to use erubis with ruby 2.1?

Thanks Abhay

abrocks
  • 171
  • 13
  • There is a [Pull Request](https://github.com/kwatch/erubis/pull/5) for Ruby 2.1 support, but it is not merged (yet). BTW, did you run into performance issues already in your application? If so, are you sure that the template engine is the bottleneck? And do you have a benchmark suite to compare different template engines? – zwippie Dec 23 '14 at 12:55
  • @zwippie Thanks for the quick reply. We are builiding a library in which we have to render around 2 million templates + db operation through script(not web request rendering) in as lower time as possible. Slow performance of ERB rendering came to our notice in our web application which serves 2 million users per month. Erubis boast itself as 3 times faster. This could be significant improvement in our case. – abrocks Dec 23 '14 at 15:19
  • Ok, that sounds like a fair situation to look for the fastest template engine. BTW: Erubis is present in my `Gemfile.lock` as a dependency of some other gems, and it seems to work with my Ruby 2.1 setup. – zwippie Dec 23 '14 at 15:56
  • Thanks @zwippie. That's helpful. Also can you suggest is the erubis community active? Would you recommend the gem in my use case. – abrocks Dec 24 '14 at 09:07
  • Well I just found out that erubis is already included in Rails 3+! It is listed as a dependency [in ActionView](https://github.com/rails/rails/blob/4-1-stable/actionview/actionview.gemspec), so perhaps you are already using erubis?! – zwippie Dec 24 '14 at 09:14
  • @zwippie find my benchmark analysis below. – abrocks Dec 29 '14 at 14:44
  • @zwippie any suggestions how can we make erubis secure considering template will be edited by end users? for e.g. below code is displaying user data in my rails application. Erubis::Eruby.new("I am <%=first%> <%=last%> <%=User.all.inspect%>").result({first: 'firstname', last: 'lastname'}) – abrocks Feb 20 '16 at 15:20

1 Answers1

4

I ran benchmark between ERB and erubis rendering with below code snippet.

erubis_render_time =  Benchmark.realtime {

  template_content = File.read("#{Rails.root}/app/views/web/email_templates/erubis_benchmark_test.erb")
  1000.times do |j|
    email_body = Erubis::Eruby.new(template_content).result({welcome_mail_cta: "Shop Now", welcome_mail_string: "Welcome. Your account is activated"})
  end
}


template_path = "/web/email_templates/benchmark_test"
erb_render_time = Benchmark.realtime {
1000.times do |j|
  email_body = ActionController::Base.new.send(:render_to_string,
                                              :template => template_path,
                                              :layout => false,
                                              :locals => {:data => {welcome_mail_cta: "Shop Now",
                                                                    welcome_mail_string: "Welcome. Your account is activated"
                                                    }
                                                          }
                                              )
end
}

As per above benchmark suite Erubis is 10-15 times faster then ERB rendering.

Adobe
  • 12,967
  • 10
  • 85
  • 126
abrocks
  • 171
  • 13