50 Tips to Boost Performance of an ASP.NET Application – Part 2

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.

 

 

36. Error Handling in Global.asax

Although implementing an error handling in Global.asax does not necessarily increase the performance of the application, it helps to identify unexpected exceptions that might occur in the application.

37. Efficient String Handling

We should keep in mind the followings when using string object in code.

1. Use the Equals method of the string object instead of the == operator for comparison purposes.

2. Use String.Empty for creation of an empty string instead of “”;

3. Use String.Length for checking an empty string.

4. Do not create string instances inside of loop.

5. Use StringBuilder for concating more than 4 strings.

38. Selection of Collection object

While working with the collection object, we should choose a proper collection object just to avoid the boxing and unboxing overhead. Strongly typed arrays are always preferable otherwise a generic list is a better choice to avoid boxing and unboxing. For example:

List<string> list=new List<string>();

List.Add(“India”);

List.Add(“Japan”);

List.Add(“China”);

List.Add(“USA”);

39. Build Assemblies in Release Mode

When deploying assemblies to the production server build the assemblies in Release mode.

40. Kernel Caching

Use kernel caching for IIS6.0 or higher.

41. Obfuscation

Obfuscated assemblies to reduce size and improve performance.

42. Application Pooling

An application pool can cover one or more applications but it is optimal to maintain an application pool for each application separately to improve maintainability and scalability. Proper recycle intervals can be set for our application pools in IIS so that the connectivity in the web server will never be lost.

43. Compiled LINQ queries

In compiled LINQ queries the query plan is cached in a static class. So there is no need for building the query plan from scratch as  LINQ uses the query plan from the static class which acts like  a global cache. As a result there is a significant performance gain.

44. PLINQ

When there are several processors in a system, PLINQ can help to speed up execution of various operations such as processing a collection of items from a database.

45. Use Ajax

In a data driven and interactive web application we should avail of the performance improvements of ASP.NET Ajax.

46. Minimize the content in UpdatePanels.

UpdatePanels are a very powerful and easy-to-use but they add a lot of overhead. Instead of having a single UpdatePanel wrapping a lot of content, only include the parts of the page that need to be updated in the UpdatePanel. Use multiple UpdatePanels on a page if necessary.

47. Literals Not Labels

There is a temptation to use only labels when rendering text on a page. Labels always add a <span> tag around the text, instead consider using Literals which add no additional markup.

48. Use The Repeater Control

The Repeater control is lighter-weight than other alternatives such as DataLists or GridViews, so where possible use the Repeater control.

49. Avoid Nested User Controls

User controls are a powerful way to re-use page elements but they do add a performance cost to page rendering. Nesting user controls is an even bigger drain on resources and should be avoided where possible.  

50. Use SqlDataReader Instead of Dataset :

DataReader is faster than DataSet. While reading a table sequentially we should use the DataReader instead of  DataSet. The DataReader object creates a read only and forward only stream of data that will increase application performance because only one row is in memory at a time.

 

  If you are looking for additional resources  –  10 Best Practices to Boost Website Speed on CloudHostingMag.com is a good place to start.]]>

Leave a comment

Your email address will not be published.