0

I have a table "State", consisting of a few thousand records, and I'm printing each of their names on one page. Would adding a dynamic tooltip to each add noticeably to the load time of the page? In other words, would there be a significant performance difference between these two blocks of code?

<%= State.all.each do |state| %>
  <%= state.name %>
<% end %>

or

<%= State.all.each do |state| %>
  <%= link_to state.name, "#", title: "(" + state.weather + ")" %>
<% end %>

For the record, weather is a instance method calculated by adding two State attributes together:

def weather
  self.snow + self.rain
end
Joe Morano
  • 1,715
  • 10
  • 50
  • 114
  • You might consider paginating your results and making them searchable instead of having to put everything in a single page. – Tamer Shlash Mar 12 '15 at 04:54

1 Answers1

1

If you want to know you can easily check the log, it shows you the active record time and the view rendering time

It would look something like this

Completed 200 OK in 539ms (Views: 486.9ms | ActiveRecord: 1.8ms)

The first 200 means that the request was a success, the 2nd number 539ms is the total request time, which in this case is '539 milliseconds', then there's a break down of that time, 486.9ms was spent in the view rendering, and 1.8ms is spent in the ActiveRecord queries

Test both cases, I don't think you'll find any huge difference but you should check

PS: you should move the query call to the controller, just for good convention

# controller
@states = State.all

#view
<%= @states.each do |state| %>
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • What do the various numbers mean in the log? – Joe Morano Mar 12 '15 at 05:16
  • well the first 200 means that the request was a success, the 2nd number `539ms` is the total request time, which in this case is 539 milliseconds, then there's a break down of that time, `486.9ms` was spent in the view rendering, and `1.8ms` is spent in the activerecord queries, it's well known that the most expensive operation in rails is the view rendering, that's why caching views improves the performance a lot – Mohammad AbuShady Mar 12 '15 at 05:22