3

I am trying to optimize my code as much as possible. I use a lot of partial files like this:

@if (Model.PageMeta.Sidebar == PageMetaSidebar.Small) { Html.RenderPartial("_SmallSidebar"); }
..
..
..

Can someone tell me if there is a performance overhead with this. I understand that Razor views are compiled. Is it the case that when the page is displays there is another disk read to get the data for each of the partial files that I use. If that's the case then how much additional overhead could I expect with for example 5 RenderPartials on my layout page.

tereško
  • 58,060
  • 25
  • 98
  • 150
Janese
  • 741
  • 1
  • 6
  • 6

3 Answers3

2

There will be no noticable performance hit at all here as the partials are just pulled in on the asp.net web server before streaming the resultant HTML back to the browser. This is not an expensive disk read to do and won't appear any slower than if it was a single cshtml. Obviously partials should be used if the same partial view is reused in many views. If only used in a single view then it's just a matter of clarity splitting it into a separate partials to separate parts of your model into different views.

Note you can also just use:

@Html.Partial("YourPartial")

rather than using RenderPartial. This will look in the local view folder then in shared if not found.

Chris Snowden
  • 4,982
  • 1
  • 25
  • 34
  • Isn't Partial slower then RenderPartial because it returns a string? Or is that not much of a performance hit either? – Alex Apr 02 '14 at 22:51
  • You're right that RenderPartial is apparently better for performance. See http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction for details. – Chris Snowden Apr 04 '14 at 12:42
0

I don't think using RenderPartial ~5 times is going to have a significant enough impact to design your pages poorly. If it makes sense to pull the logic out (makes the page cleaner, used by multiple views, etc.) then do it. If you notice significant performance issues then you should look at them at that time, but don't prematurely optimize and create a poor design because you THINK it might slow something down.

Dismissile
  • 32,564
  • 38
  • 174
  • 263
0

If you like to get a better understanding of potential performance hits you might want to play around with the mvc-mini-profiler.

Please note though that I'm not advocating pre-mature optimization. However, using profiling tools might give you a better understanding of potential bottlenecks, thus helping you avoid them in the future.

mrydengren
  • 4,270
  • 2
  • 32
  • 33