--- title: Are rails partials actually slow? date: 2018-03-04T21:38:04Z status: published layout: ../layouts/Post.astro --- At my previous job, we were told not to use partials to clean up our view code, because each rendering of a partial would add around 10ms to the overall response time. After I was told this I did a quick test in my dev environment, but I'd never actually tested the claim on production ... until today! ## The setup I created a new rails project and generated a scaffold I called `things`. On the index page, underneath the auto-generated table, I put the following: ```erb <% if params[:manypartials].present? %>

Many partials:

<% 1000.times do %> <%= render "simplepartial", iterations: 1 %> <% end %> <% else %>

One big partial:

<%= render "simplepartial", iterations: 1000 %> <% end %> ``` I then created the partial `simplepartial` and added this code: ```erb <% iterations.times do %>

This is a simple partial. Hello from the partial!

<% end %> ``` ## Results I launched the dev server with `rails s` and the production server with `rails s -e production`. I ran `time curl "http://0.0.0.0:3000/things?manypartials=true" > /dev/null` and `time curl "http://0.0.0.0:3000/things" > /dev/null` to make the requests. (The pipe to `/dev/null` is to reduce the time spent printing stuff to the terminal.) | | Many partials | One big partial | | ---------- | ------------: | --------------: | | Dev | 44.365s | 0.852s | | Production | 0.273s | 0.152s | So it seems that rendering 1000 partials will cause around 100–200ms of extra rendering time. That's certainly not great, but it may not be too high a price to pay for HTML templates not growing to thousands of lines.