Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderAction vs RenderPartial performance

According to Brad Wilson, RenderAction is slower than RenderPartial.

However, has anyone got any statistics that show the difference in performance?

I'm in the process of developing an application where pages are composed of "Widgets".

I have two choices:

Composition at the View Level

Call RenderAction for each widget. This is by far the easiest approach but does mean that we're performing a full MVC cycle for each widget.

Composition at the Controller Level

Compose one ViewModel for the page that contains the data we need for each widget. Call RenderPartial for each widget. This is much more complicated to implement but does mean we'll make only one MVC cycle.

I tested the above approaches with 3 different widgets on a page and the difference in render time was 10ths of a second (hardly worth worrying about).

However, has anyone got any test results more concrete than this, or perhaps experience trying both approaches?

like image 205
Ben Foster Avatar asked Jun 27 '12 14:06

Ben Foster


People also ask

What is the difference between RenderPartial and RenderAction?

RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.

What is the difference between RenderPartial and partial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

Which of the following differentiates action from RenderAction?

The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.


2 Answers

I've recently worked on an application that was experiencing performance issues, and found a view that was making four calls to RenderAction, plus another one in the layout. I found that each call to RenderAction--even when I added in a dummy action that returned an empty view--took around 200-300ms (on my local machine). Multiply by the number of calls and you have a huge performance hit on the page. In my case there were four calls causing about a second of unecessary server-side overhead. By comparison, calls to RenderPartial were around the area of 0-10ms.

I would avoid using RenderAction wherever possible in favor of RenderPartial. The controller should be responsible for returning all necessary information. In the case of widgets, if you need multiple actions for several widgets, I would try composing them into one action so the RenderAction overhead only occurs once, though if your site performs adequately I'd keep them separate for a cleaner design.

Edit: I gathered this information using MiniProfiler and hitting the site. It isn't super accurate but it does clearly show the differences.

Edit: As Oskar pointed out below, the application in question likely had some intensive code that runs for each request in global.asax. The magnitude of this hit will depend on the application code, but RenderPartial will avoid executing another MVC cycle altogether.

like image 57
mao47 Avatar answered Sep 27 '22 23:09

mao47


I'd suggest 2 more options, both require to compose the view model at Controller level and both can work together (depending on the data)

  1. Html.DisplayFor() - display templates
  2. Helpers via extension methods

Option 2 works very well if you want to keep those widgets in different assemblies, after all they're just functions returning a string. I think it has also the best performance, but of course you lose the 'designer friendly' templates. I think it's important to consider the maintainability aspect, not only raw performance (until you really need it, and even then, caching is more helpful).

For small stuff (date or name formatting etc) i'd use helpers, since the html is usually a span with a class, for more complex stuff I'd use the display templates.

like image 41
MikeSW Avatar answered Sep 27 '22 23:09

MikeSW