title: ASP.NET MVC partial views slow?
body: <p>I just happen to check the performance of an ASP.NET MVC application we are building. I was going to insert a partial view into a loop, and just out of curiosity I checked how long it took to render the page. The result was not good.</p>
  
  <p>I need to do more conclusive investigation, but just in case there was somebody with similar issues or more insight, here is what I have so far. First, I should say that all results and measurements were done after multiple page loads and that I have set <code>&lt;compilation debug="false"&gt;</code> in my web.config.</p>
  
  <ul>
  <li>It seems that a single render partial incurs about 5ms hit (at least in my environment). When I inline the actual content of the partial view, I get practically 0ms.</li>
  <li>When I include an empty partial view to a loop of about 70 elements, the total render time increases by ~ 60ms. So there is some caching presumably, but it's not ideal.</li>
  <li>I debugged ASP.NET MVC, and found out that partial views are cached, but it only caches the paths to the ascx's. The actual views are then instantiated every time using the <a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.createinstancefromvirtualpath%28v=VS.90%29.aspx" rel="noreferrer">BuildManager.CreateInstanceFromVirtualPath</a> method.</li>
  <li>And now the interesting bit: When include the same partial view using the WebForms syntax (<code>&lt;my:UserContol runat="server" /&gt;</code>), the extra 60ms go away.</li>
  </ul>
  
  <p>So based on the observations above, it seems the culprit is the BuildManager.CreateInstanceFromVirtualPath method. Maybe, it was not meant to be called multiple times. Webforms presumably don't use it; or use it somehow only once for each ascx?</p>