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

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.