开发者

Speed difference between anonymous class and IDictionary<string,object> for htmlAttributes in ASP.NET MVC

I am trying to optimize my ASP.NET MVC application using some techniques that include URL generation tweaking a la: http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html

If the speed difference is so large between using RouteValueDictionary in place of anonymous classes, then should I also look to use Dictionary in place of anonymous classes when defining html attributes?

For example, should I do this:

Html.ActionLink("LinkName", "Action", "Controller",
                new RouteValueDictionary { { "id", Model.Id } },
                new { @class = "someCSSClass" })

or should I further optimize by doing this:

Html.ActionLink("LinkName", "Action", "Controller",
                new RouteValueDictionary { {开发者_运维问答 "id", Model.Id } },
                new Dictionary<string, object> { { "class", "someCSSClass" } })

I know it's even faster to use Url.Action, or better yet to use the RouteLink technique, but I'm just wondering whether anonymous classes should be completely avoided for the sake of speed.


Yes, it's faster to use the Dictionary.

Is it fast enough to make a difference? Only a profiler can tell you that, for your application. I'd suggest that if it actually makes a difference then you should be caching your view results anyway.

I tend to stick with the Dictionary versions, though, because the strong typing helps to cut through the insane mess of overloads to ActionLink. Passing an object makes it way too easy to end up with the wrong overload. The speed is just a bonus.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜