Rails 4: why is there one way to render partial so fast?

I'm not sure if this is a particular issue with Rails, so I also noted it ruby.

I present a collection of entries eventthrough partial. However, I found that partial execution of a partial result leads to dramatic differences in performance.

Both versions use the same data, the only thing that changes is the code used to render partial files.

Why is heck one version that is 4 times faster than another? Makes me wonder what other performance hits I accept ...

Slow version (total request time 950 ms):

<% 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 (total request time 250 ms):

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

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

1: x ( ). , html x ( ).

2: html ( ).

+3

Source: https://habr.com/ru/post/1679090/


All Articles