I have code that passes almost 1MB of data from my controller to the view through a viewmodel each time a new page is called. I could optimize this slightly but I am wondering if this would be worth doing as the flow of data is all internal.
Typical of what I am doing in the controller is that I am getting all test results from an Azure datastore and then putting them in a new instance of a class. I am then passing this class and others onto a view. I guess I am not sure. Would the data be passed by reference or would actual data be moved from one place to another?
Anyone have any experience with this side of performance tuning for MVC3?
Here's a made up example. In this example it's nice and easy to pass the "TestData" class and contents to the view but then I just need a couple of items from this class. So I'm wondering if I should add logic in the controller and add fields in the view model for these items or just not bother and move across all the class data including the data I don't need.
public class testIndexViewModel
{
public string Url { get; set; }
public PageMeta PageMeta { get; set; }
public TestData TestData { get; set; }
}
Thanks,
The ViewModel
class that you create gets added to the ViewDataDictionary
which your compiled custom View
class accesses through its base System.Web.Mvc.ViewPage<ViewModelType>
class. All that means is that I'm pretty sure your ViewModel
- once it's created - is always accessed by reference, and not copied around the place.
The only performance issue you could come across (I suppose) would be consequent to creating a 1MB object in the first place; how many of these objects are you likely to be creating, and how often?
Personally, I'd not worry about performance optimisation without first load testing and noticing that it's actually causing a problem. If it does and your application only really needs a fraction of the data at any one time, you can build in an optimisation then.
Finally, if you have code in your View to sift through your 1MB of data and pick out the sections you want to display, you might want to only pass the View the data it needs in order to make the code more readable, and better separate the Controller and View's responsibilities.
http://en.wikipedia.org/wiki/Premature_optimization#When_to_optimize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With