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
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| %>