Dan L Dan L -4 years ago 51
Ruby Question

Rails 4: why is one way of rendering partials so much faster?

I'm not sure if this is a Rails specific issue, hence the reason I tagged it

ruby
as well.

I'm rendering a collection of
event
records via a partial. However, I found that rendering the partial differently results in drastic performance differences.

Both version use the exact same data, the only thing changing is the code used to render the partials.

Why the heck is one version consistently 4x faster than the other? Makes me wonder what other performance hits I'm taking...

Slow version (950ms total request time):

<% events.each do |event| %>
<%= render partial: "events/event", locals: { event: event } %>
<% end %>

# Log output
Rendered events/_event.html.erb (1.1ms)
Rendered events/_event.html.erb (1.1ms)
...


Faster version (250ms total request time):

<%= render partial: "events/event", collection: events, as: :event %>

# Log output
Rendered events/_event.html.erb (58.7ms)

Answer Source

example 1: you are rendering a partial x times (depending on events). which means you are compiling html x times(once each time the loop runs). which is slow

example 2: you are rendering one partial with a collection of events the html is compiled once(as there is only one partial). which is fast

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download