A Rails concert-listings page renders 500 gig cards with render inside an each loop and is slow; what does a partial cost per call, and what helps?
answer
- per-call render overhead
- one instrumented call for a collection
- nested partials multiply
- queries inside the card dominate
- measure before replacing partials
basics
~20 sEach Rails partial render pays for option handling, a template lookup and instrumentation; a collection render pays that once for the whole list. Queries and nested partials inside each card usually cost more and should be fixed first.
solid answer
~40 sCalling `render "gig", gig: gig` 500 times inside `each` repeats the per-call work 500 times: building a renderer, resolving the partial through the lookup context (cached, but still a lookup), instrumenting the render and, in development, logging a line. `render partial: "gig", collection: @gigs` or `render @gigs` resolves the template once, runs one instrumented collection render and loops internally, so the overhead per card is smaller. The bigger costs usually sit inside the card: a query per card for `gig.venue`, helper-heavy markup, or partials nested inside the partial, which multiply the per-call cost. The fix order is: measure with the log and a profiler, remove per-card queries, switch to collection rendering, flatten deep nesting, and only then consider caching or component classes.
code
ruby · 4 lines# app/controllers/gigs_controller.rb
def index
@gigs = Gig.upcoming.includes(:venue, :band).order(:starts_at).limit(100)
endgo deeper
Know that rendering a partial inside a loop is slower than rendering the collection in one call.
Explain the per-call work of render, how collection rendering amortises it, and why queries inside cards need preloading.
Diagnose a slow list page from logs and profiles, fix queries and nesting first, and justify any move to caching or components with numbers.
Set performance budgets for list pages and decide when pagination, caching or a component architecture is the right lever.
## Where the time goes A partial is not free. Every `render` call from a view does a small amount of fixed work before the partial's own body runs: 1. **Interpreting the options**: is this a partial, an object, a collection, which locals? 2. **Finding the template** through the lookup context with the current details (locale, formats, variants, handlers). Results are cached per set of details, so this is a hash lookup in production rather than a disk scan, but it is repeated per call. 3. **Instrumentation**: an `ActiveSupport::Notifications` event per render, which the log subscriber turns into a `Rendered gigs/_gig.html.erb` line in development. 4. **Running the compiled method** with a fresh output buffer, then appending the result to the page. For one card none of this matters. For 500 cards, each of which renders three more partials for the venue, the price tag and the ticket button, it is 2,000 render calls per page. ## Loop versus collection rendering | Approach | Template lookup | Notifications | Log lines (dev) | |---|---|---|---| | `@gigs.each { render "gig", gig: }` | once per call | one per card | one per card | | `render partial: "gig", collection: @gigs` | once | one for the collection | one, `[500 times]` | | `render @gigs` (homogeneous) | once | one for the collection | one | The collection form also hands each card its counter and iteration locals, and it is the form that collection caching builds on, which is a separate topic. ## What usually costs more than the partial - **Queries per card.** `gig.venue.name` inside the partial runs a query per card unless the relation preloaded venues. Collection rendering does not add eager loading for you; the controller's relation has to. - **Nested partials.** A card that renders four sub-partials pays the per-call overhead five times per gig. Inline tiny fragments, or render the sub-partials as collections where they repeat. - **Expensive helpers.** Date formatting, currency formatting and URL generation in every card add up; compute shared values once outside the loop and pass them in `locals:`. - **Huge pages.** Five hundred cards is often a product question: paginate or load more on scroll. ## A measurement-first workflow 1. Read the development log for the request: total view time versus Active Record time, and how many `Rendered` lines appear. 2. If Active Record time or the query count is high, fix preloading first. 3. Replace `each` + `render` with collection rendering and compare. 4. Profile the remaining view time with a sampling profiler to find the expensive helpers or nested partials. 5. Only then reach for caching of the cards, or for component classes if the team prefers them for other reasons; components are not a guaranteed speed-up and should be measured like anything else. ## Strict locals and compile cost Separately from per-render cost, a partial without a `locals:` signature is compiled once for each distinct set of local names it is called with. That is a warm-up and memory cost rather than a per-request one, and declaring strict locals keeps it to one compiled method per partial. ## Interview framing A strong answer separates **per-call overhead**, fixed by collection rendering and flatter partial trees, from **per-card work**, usually queries and helpers, and insists on measuring before replacing partials wholesale.
- How do you tell from the development log whether a list page uses collection rendering?A collection render logs one line such as `Rendered collection of gigs/_gig.html.erb [48 times]`. A loop logs one `Rendered gigs/_gig.html.erb` line per card. The same log shows the Active Record time and query count, which tell you whether queries dominate.
- Would switching every card to a component class fix the slowness?Not by itself. If queries or heavy helpers inside the card dominate, they move into the component unchanged. Component classes may reduce per-render overhead and improve structure, but the change should be measured, and preloading and pagination usually matter more.
- Rails re-reads the partial file from disk on every render in production.
- Collection rendering preloads the associations each card reads.
- Partials have no measurable cost, so nesting depth never matters.
- Replacing partials with components always makes a list page fast.
- Collection rendering renders the cards in parallel threads.