简体   繁体   中英

Checking condition before/after RenderPartial performance

Let's say there's a RenderPartial that checks if a certain condition is true, then renders the page.

Is there any performance benefit to check for the condition first, then call RenderPartial ? I'm sure that this technique is a performance benefit for RenderAction 's, since we can essentially short circuit going through the MVC lifecycle step. But I'm thinking since RenderPartial "goes" straight to the view, it might not have the same benefit.


Example (Checking the Condition in RenderPartial):


myPage.cshtml

@model Namespace.AdObject

@{ Html.RenderPartial("~/Views/Ad.cshtml", Model); }

~/Views/Ad.cshtml

@model Namespace.AdObject

@if (Model != null && Model.AdScript != null)
{
    // render the ad
}

Example (Checking the Condition Before RenderPartial):


myPage.cshtml

@model Namespace.AdObject

@if (Model != null && Model.AdScript != null)
{
    Html.RenderPartial("~/Views/Ad.cshtml", Model);
}

~/Views/Ad.cshtml

@model Namespace.AdObject

// render the ad

RenderPartial() is a void method that writes to the response output stream. The HTML output generated by executing the partial view is rendered into the calling (or parent) view. Even If model is null , write will be time consuming and it reduces efficiency. So checking the condition before RenderPartial is better in terms of performance.

I would say checking for before the RenderPartial call is better. Even if your Partial view is small it still comes down to an extra file system IO call to the .cshtml file from the Web Server which is a bottleneck to performance. When you check first you can avoid the IO call all together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM