I use a foreach
in my View to loop over my strongly-typed Model and display a table. With only 25 rows and 7 columns, it takes about 280 msec. This seems slow. Is there some performance trick I should know for using loops in Views?
EDIT: My Controller takes data from an Azure Table and feeds it to a View using a ViewModel pattern. I don't know if this matters, but my Views are in VB and my Models are in C#. So, they're in separate projects. I'll come up with a slimmed down example to post, but I'm running out the door now and will have to get to this later this evening. I was hoping I'd catch the StackOverflow crowd before people went home for the weekend, so my original post was made quickly without example code.
EDIT: I confirmed with Fiddler that there's no lazy loading happening. There's no Fiddler activity during the rendering of the View.
EDIT: If I feed a View with data from an Azure table, it takes 280 msec. If I feed the same View with fake data that looks just like real data from an Azure table, it takes 60 msec. In either case, the Controller just populates a ViewModel object and passes it to the View. The same ViewModel class is used in both instances. I don't get it.
EDIT: I think I figured it out. Perhaps this would have been obvious to everyone else if I had included code at first. Here's my ViewModel:
public class EmployeeChildrenViewModel
{
public Employee employee;
public IEnumerable<Child> children;
}
If I pass the above ViewModel to my View, the foreach
takes 280 msec. If I populate the above ViewModel with children.ToList()
first, then the View only takes 60 msec. However, upon further investigation I see that the overall page load time is the same in either case. I guess it doesn't matter if the IEnumerable is iterated over in my Controller or my View since the overall effect is the same. I'm still not sure what iterating over children
does since I know for sure that it's not hitting the database at that point, as confirmed with Fiddler.