11
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 much slower.
test.cshtml:
@{
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
for(var i=0; i<1000; i++) {
var str = "my string #" + i;
@Html.Partial("_MyPartial",str) // replace with @str on second test
<br />
}
stopwatch.Stop();
<br />
@: Time elapsed (ms): @(stopwatch.ElapsedMilliseconds)
}
_MyPartial.cshtml:
@Model
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.
If anyone wants to confirm or criticize my experiment, you are very welcome.