IEnumerable<Participant> participants=GetParticipants();
if(products.count()==0)
{
SomeControls.Visible=false;
}
else
{
SomeControls.Visible=true;
}
The wrong thing with the above code is that Count() is a Linq extension Method that literally iterates through every item in an enumerable( here it is participants). In other words it won't short circuit after it finds more than zero items and will continue iterating through every single item. And while, that one may be fast operation in Dev environment but it will be a slow process in production. This is because of Linq's deferred Execution.
How we can refine the above Code
The above Code can be refined by changing it into as:
IEnumerable<Participant> participants=GetParticipants();
if(products.Any())
{
SomeControls.Visible=false;
}
else
{
SomeControls.Visible=true;
}
In the above code once the expression finds more than one item, it simply stops iterating and just increasing the Optimization time.
Happy Coding.........!
No comments:
Post a Comment