Performance: Fragment caching
Fragment caching improves page load speed by storing parts of the page output to avoid repeated rendering.
<% cache('comments_fragment') do %> <%= render 'comments' %> <% end %>
<%= render 'comments' %> repeated multiple times without caching| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No caching, repeated partial renders | High (many nodes rendered repeatedly) | Multiple reflows per partial | High paint cost due to delayed content | [X] Bad |
| Fragment caching with stored HTML | Low (cached HTML reused) | Single reflow on initial load | Low paint cost, faster content display | [OK] Good |
cache 'key' do ... end block in ERB.<% cache 'unique_key' do %> ... <% end %> matches the correct block form. Others have wrong block usage or missing do.cache 'key' do block [OK]<% cache 'post_1' do %> <h1>Post Title</h1> <p>Post content here.</p> <% end %>
<% cache 'user_42_profile' do %> <p>User profile info</p> <% end %>
Pass the test and this pattern moves unknown