views:

246

answers:

1

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.

+2  A: 

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.

Craig Stuntz
+1 for mentioning _per application_ profiling, although this whole debate seems like micro-optimization to me. If your site isn't under load then I doubt the performance gain is all that meaningful in terms of net throughput. And if your site IS under load then chances are that far more resources are being spent servicing DB requests than rendering views, so the net gain in throughput is probably still low. Only a profiler and a representative load test will say for sure :)
Seth Petry-Johnson