title: Performance cost of using Partials in Razor views
body: <p>I like using partials in my Razor views. It makes the code nice and clean. But is there any significant performance cost of using partials? I've created a simple test. It shows that using partials is <strong>much</strong> slower.</p>
  
  <p>test.cshtml:</p>
  
  <pre><code>@{
     var stopwatch = new System.Diagnostics.Stopwatch(); 
     stopwatch.Start();         
     for(var i=0; i&lt;1000; i++) {
        var str = "my string #" + i;        
        @Html.Partial("_MyPartial",str)  // replace with @str on second test
        &lt;br /&gt;        
     }
     stopwatch.Stop();
     &lt;br /&gt;
     @: Time elapsed (ms): @(stopwatch.ElapsedMilliseconds)
  }
  </code></pre>
  
  <p>_MyPartial.cshtml:</p>
  
  <pre><code>@Model
  </code></pre>
  
  <p>The code with the partial executed in 340ms, while the inline @str code was showing 0 or 1 ms.  It is really shocking for me because it means I should get rid of all my cute partials, at least of those in loops. </p>
  
  <p>If anyone wants to confirm or criticize my experiment, you are very welcome.</p>