Introduction
Often we come across code written in older versions of .NET (for example, 1.0 and so on) where the performance of the application is very slow. Most of the developers are in need of improving the performance of the existing code even without migrating to the latest versions.
This article has a few tips to handle such scenarios.
Places to look For:
When you want to work on performance improvement of a web page, the key areas to look for are:
- Code
- Database
Code
In aspx pages or ascx pages you can look for the following improvements.
- Server roundtrips
Look for unnecessary round trips to the server, rather replace the same with client code (for example, JavaScript or jQuery).
- DataControls
By default view state is enabled for all data controls. Disable the viewstate by setting EnableViewState = false;
- Check for paging
If paging is not implemented in your data control, handle that first.
- Sorting and Filtering
Move all the Filters and search criteria to DB. Sorting and filtering can be faster in DB rather than in memory.
- Minimize the use of View State
- Refine the search for data
Rather than blindly getting all the data from the database to the grid, you should have at least one filter criteria, so that data retrieval is faster.
- Stored Procedures vs Table data
Use Stored Procedure calls rather than direct table hits. This will improve performance in a great way.
- Caching
Avoid getting too many records from the database and holding them in the cache. Over a period of time, the cache will be too huge and performance will degrade. Instead, cache small chunks of data.
- Exception handling
Avoid having too much exception handling. Exceptions degrade the performance of the application. When you already know that an exception will occur, why not avoid it, rather than making a try..catch block to handle it.
Don't use:
- Try
- {
- int div = a/b;
- }
- Catch(Exception e)
- {
-
- }
Instead use:
- If (b>0)
- {
- int div = a/b;
- }
Database
- Pagination
Have paging incorporated in the Stored Procedures, For example In a SQL Stored Procedure, you can:
- CREATE PROCEDURE [dbo].[GetPolicies]
- ( @PolicyID INT = NULL,
- @UserID INT = NULL,
- @PageNumber INT = 1,
- @NoOfRecords INT = 50
- )
- BEGIN
- …………
- END
- Indexing
Indexing is the key to performance. Indexing the tables on a proper key when there is huge data to be fetched, will largely improve the performance.
- Limit data fetch
Avoid sending a huge amount of data over the network.