With ASP.NET MVC's BindingModel concept, I would love to use the following syntax in my HtmlHelper methods: Html.TextBox(() => product.Name) so I wouldn't need to pass the "Name" string every time. This notation less error prone...
I did some tests to check the performance of passing Expression<Func<T>> objects instead of the actual values...
Each of the below lines was executed 10'000'000 times (except the third one, which I executed only 1000 times and multiplied the resulting miliseconds by 10'000). DataClass.DataItem holds the value 2.
Plain value passing: 22 ms
result += dataClass.DataItem;
Reflection: 13703 ms
PropertyInfo pInfo = dataClassType.GetProperty("DataItem",
typeof(int));
result += (int)pInfo.GetValue(dataClass,
null);
Expression trees: 6'240'000 ms
result += ((Expressionint>>)(() => dataClass.DataItem)).Compile()();
Lambda function: 44 ms
result += ((Func<int>)(() => dataClass.DataItem))();
So the expression tree's performance is way too bad that we could use it within the helper methods...
Does anybody know if there is another way to achieve the above goal but with better performance?
tolstoi
Member
2 Points
18 Posts
Performance of using HtmlHelper methods with expression trees
Oct 27, 2008 10:46 AM|LINK
Hello,
With ASP.NET MVC's BindingModel concept, I would love to use the following syntax in my HtmlHelper methods: Html.TextBox(() => product.Name) so I wouldn't need to pass the "Name" string every time. This notation less error prone...
I did some tests to check the performance of passing Expression<Func<T>> objects instead of the actual values...
Each of the below lines was executed 10'000'000 times (except the third one, which I executed only 1000 times and multiplied the resulting miliseconds by 10'000). DataClass.DataItem holds the value 2.
Plain value passing: 22 ms
result += dataClass.DataItem;
Reflection: 13703 ms
PropertyInfo pInfo = dataClassType.GetProperty("DataItem", typeof(int));
result += (int)pInfo.GetValue(dataClass, null);
Expression trees: 6'240'000 ms
result += ((Expressionint>>)(() => dataClass.DataItem)).Compile()();
Lambda function: 44 ms
result += ((Func<int>)(() => dataClass.DataItem))();
So the expression tree's performance is way too bad that we could use it within the helper methods...
Does anybody know if there is another way to achieve the above goal but with better performance?
Thanks for any help,