Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-2

How efficient is the current_page? helper method?


I'm curious about the efficiency of the Rails current_page? helper method. I'm using it in a view, roughly as shown below:

  <% if current_page?(:action => "foo") %>
    <dt>Label 1:</dt>
  <% else %>
    <dt>Label 2:</dt>
  <% end %>

and

  <% if current_page?(:action => "foo") || current_page(:action => "bar") %>
    <dt>Label 1:</dt>
  <% else %>
    <dt>Label 2:</dt>
  <% end %>

But would it be more efficient to make this switch some other way? For instance, would it be more efficient to set an instance variable in my controller actions for foo and bar, then check <% if @foo || @bar %>?

And is there any difference in the efficiency between Rails 2 and Rails 3?


Solution

  • I can't speak to the difference between Rails 2 and Rails 3, but looking at the source code in ActionPack, it doesn't look like the method is doing anything particularly complicated. url_for could be slightly inefficient (I don't know offhand), but unless you're doing a huge number of loops over this partial, optimizing this is probably not going to save you a noticeable amount of time.

    That said, it would be easy to do some basic benchmarks -- t = Time.now; loop 1000 times over one version; v1_time = t - Time.now; t = Time.now; rinse and repeat with the other version. If you do it, let me know what you find.

    All that said, it seems to me that it would be probably be cleaner conceptually to have your controller methods set appropriate flags, if the flags could be expressed as concepts not directly related to the view. I'd be curious what others think.