Topic: Loop and array filtering takes alot of time!

Im basically looping a given month's days

My testing:
@episodes = 500-600 records

<%- @start_day.upto(@end_day) do |date|  -%>
  <% show_episodes = @episodes.select {|e| e.air_date == date} %>
  <%= render :partial => "partials/calendar_cell", :locals => {:episodes => show_episodes} if show_episodes.size > 0 %>
<%- end -%>

even WITHOUT actually rendering the partial, the view takes about 5000-6000 ms! (DB-Query takes about 100-200ms including joins)

are ruby arrays actually that slow, or what is going on?

Re: Loop and array filtering takes alot of time!

Regrettably, Ruby arrays can indeed be that slow.  Based on a 5-6 second lag I'm guessing your running Ruby 1.8.6/1.8.7.  If upgrading to 1.9.1 is an option I would suggest that.

In an application I'm working on right now performing an array.collect operation over 800 objects added roughly 2.3 seconds to every request for the data.  My advice is to move:
@episodes.select {|e| e.air_date == date}
Into the database as a named_scope that will give you the array containing the data you want.

I'm Chris Chandler, a Phoenix Ruby on Rails developer and startup junkie kicking ass at Flatterline, a Ruby on Rails web application development company.