Andy
Andy

Reputation: 1002

Slow processing when render helper link_to

I'm displaying 3k users. When render helper link_to process rendering slowing. It takes a lot of time.

it is my template

- @user.each do |user|
      %tr
        %td= link_to_user_modal user
        %td.text-right
          = link_to edit_user_path(user.id),
                    class: 'btn btn-default btn-xs',
                    title: 'Edit' do
            %span.glyphicon.glyphicon-pencil

          = link_to user,
                  method: :delete,
                  title:  'Delete',
                  data:   { confirm: "Are you sure you want to delete #{user}?" },
                  class:  "btn btn-danger btn-xs" do
            %span.glyphicon.glyphicon-trash

Upvotes: 0

Views: 295

Answers (1)

m3characters
m3characters

Reputation: 2290

Rendering plain html instead of using the helper improves considerably the time needed to render. It's not as much "link_to" that burdens the rendering but more of the path_helper. Although placing raw html in the view is quicker than any of both:

<a href="the/path/the_helper/would/render/#{user.id}" target="_blank" class="whatever">Edit</a>

instead of

link_to path_helper

or even (since the helper is the biggest culprit)

<a href="#{path_helper}" target="_blank">edit</a>

check ruby-prof for the call trace - the only line that was changed was link_to to pure html with the id inserted dynamically into the href string

I also tested the tag with the href using the path helper, and though it's faster than link_to with the path helper, it's slower than with href as a string. You can also see that I didn't call .html_safe on the concanated href, which should render it even faster in the tag as well.

Upvotes: 3

Related Questions