50 Tips to Boost ASP.NET Performance – Part II

Continuing from ASP.NET Performance Tips Part 1 we now conclude with Performance Tips 26-50.

26. Batched Queries:

Queries can be used in a batch and thus network traffic can be reduced: Here is an example:

“Select EmpNo, EmpName, EmpAddress from Employee”;
“Select DepNo, DeptName From Department”;

From the above two queries it seems that there will be database hit twice .

Both the above queries can be executed in batched form and a single database hit will occur as follows:

“Select EmpNo, EmpName, EmpAddress from Employee; Select DepNo, DeptName From Department”;
27. Use IIS Compression

Page size can also be reduced using Http compression in IIS. Compression tool can also be used to reduced the size of rendered content.

28. Normalization

We should follow normalization rules in database table design but over Normalized tables can cause  excessive joins for simple requirement. We should not make excessive joins for performance overhead and hence it is better to normalize only as much as required keeping in mind the performance issue.

29. Efficient Coding

While coding we should keep in mind the below issues which are potential performance drains:

1.       Avoid use of Finalize method unless it is absolutely necessary.
2.       Make a class sealed if inheritance is not required.
3.       Avoid calling GC.Collect();
4.       Use X+=1 instead X=X+1 to avoid evaluating X twice;
5.       Use overloaded methods instead of different method names where possible.

30. Define The Scope of An Object

Defining an object’s scope properly increases efficient memory management and thus improve performance. We should keep the object scope at a lower level instead of a global level if possible because the garbage collector works more frequently on short lived object .

31. Using Loops

We should keep in mind while using loops in our code:

1.       Better to use for a loop instead of foreach.
2.       Do not create objects inside loop if not required absolutely.
3.       Calling methods and properties inside loops is expensive.
4.       Evaluating count etc. before loop starts. For example:

for(int i=0;i<Grid.Rows.Count;i++) {}can be written as
int iCount= Grid.Rows.Count; for(int i=0;i< iCount;i++){}

5.       Never write exception handling code inside loop. Keep the exceptions handling code outside of the loop for better performance.

32. Dispose Instead of Finalize

Avoid using Finalize unless it is absolutely necessary. It is normally better to dispose of unmanaged resources if no longer required.

33. Minimize Thread Creation

Avoid creating threads on a per-request basis. Also avoid using Thread.Abort or Thread.Suspend.

34. Leave Connection pooling Enable

Connection Pooling is enabled by default. It boosts application performance as existing connections can be re-used from the pool rather than created . Leave this option enabled.

35. Using Blocks

Using blocks can be used as a short form of try..finally and it should be used only for those objects that implement the iDisposable interface. Here is a code snippet.

Using(SqlConnection con=new SqlConnection(connectionString)
{
try
{
con.Open();

//some code
}
catch(Exception e)
{

}

}

In the above example,there is no need to dispose of the connection object. The Connection object will be disposed automatically when it is out of scope.
Continues…

One thought on “50 Tips to Boost ASP.NET Performance – Part II

  1. Pingback: 50 Tips to Boost ASP.NET Performance – Part I | ASP.NET 101

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>