Victor
Victor

Reputation: 13378

Performance issue: use "link_to" or "a"?

For links that don't require much controller or options, is it better to use a html tag? Will there be any performance impact on using link_to rails tag unnecessarily?

Thanks.

Upvotes: 4

Views: 1969

Answers (2)

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

Yes, you use simple html tag instead link_to if you are using links for a simple get request(not ajax or other methods like post,put etc) with less controllers or less routing defined in app

Upvotes: 0

Rafe Kettler
Rafe Kettler

Reputation: 76955

Yes. Everywhere you can write static content instead of dynamic content, you'll see better performance. If using link_to makes things a lot easier to write and understand in a particular case, then use it, but if you yourself think it's unnecessary, save the CPU time and write the a tag yourself.

This is discussed specifically as it pertains to Rails helpers in this article. According to the article:

A number of helpers in Rails core will run rather slowly. In general, all helpers that take a URL hash will invoke the routing module to generate the shortest URL referencing the underlying controller action. This implies that several routes in the route file need to be examined, which is a costly process, most of the time... For pages displaying a large number of links, I have measured speed improvements up to 200% (given everything else has been optimized).

Upvotes: 7

Related Questions