\( \renewcommand\vec[1]{\mathbf #1} \newcommand\pp[2]{\frac{\partial #1}{\partial #2}} \newcommand\dd[2]{\frac{\mathrm d #1}{\mathrm d #2}} \newcommand\d{\,\mathrm d} \)

花花花 SuperLaserNino 花花花

home · about · archive · RSS

Are rails partials actually slow?

04 Mar 2018

288 words

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:

<% if params[:manypartials].present? %>
  <p>Many partials:</p>
  <% 1000.times do %>
    <%= render "simplepartial", iterations: 1 %>
  <% end %>
<% else %>
  <p>One big partial:</p>
  <%= render "simplepartial", iterations: 1000 %>
<% end %>

I then created the partial simplepartial and added this code:

<% iterations.times do %>
  <p>This is a simple partial. Hello from the partial!</p>
<% end %>


I launched the dev server with rails s and the production server with rails s -e production. I ran

time curl "" > /dev/null


time curl "" > /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.