Reputation: 3110
I am very new to ASP.NET MVC development, started 5 days ago. You can say just in R&D phase. I like the style of development of MVC but my confusion started when one of my friend told me that ASP.NET MVC's Performance is not comparable with WebForms because of use of Reflection. e.g.:
@Html.EditorFor(model => model.FieldName)
MVC is using reflection. So according to him, we should use normal HTML Tags to overcome the reflection. e.g.
<input id="FieldName" class="text-box single-line" type="text" value="" name="FieldName" data-val-required="FieldName is required." data-val="true" />
I tried to remove by confusion by searching over the internet and found following :
ASP.NET MVC Performance
Why use lambdas in ASP.NET MVC instead of reflection?
Reflection and performance in web
But these topics does not tell me clearly whether using normal HTML will give me more performance or not and what will be the best method in MVC large scale application.
Upvotes: 1
Views: 2433
Reputation: 16636
You are virtually guaranteed that the non-reflection version will be faster as it needs to do less work. However, I expected the difference to be quite small. I have done some basic tests with two different templates.
Template 1 using Html helpers:
@for (int i = 0; i < 10000; i++)
{
@Html.EditorFor(model => model.FieldName)
}
Template 2 using plain HTML:
@for (int i = 0; i < 10000; i++)
{
<input id="FieldName" class="text-box single-line" type="text" value="" name="FieldName" data-val-required="FieldName is required." data-val="true" />
}
To test the performance, I have used the MiniProfiler library which gives us an idea on how much time is spent rendering the templates. When averaging the results, I get the following values:
The difference is thus 323.7 ms, a factor 5 difference. However, we have done our test using 10000 iterations which probably isn't default behavior in a template. If we change the templates to a more sensible default like 10 times, we get the following results:
Now you can still see some difference (3.2 ms), but in my humble opinion this is negligible. At this point, I would not be looking at the performance at all but only look at what style you prefer. If you want to completely control your HTML, go for the plain HTML version. If you want to have all the extras the HTML helpers offer (like automatically translating annotations to jQuery validation attributes), go for that method. The difference between the two methods will in most cases be low enough to not have a large impact.
Upvotes: 6