Wednesday, April 18, 2012

Performance tips for ASP.net

Performance Tips

1) Always use StringBuilder if you want to Concatenation two strings in a loop. Because if you Concatenation using the string object then both string object will be saved to memory first and then old string will be deleted and values will be read from new string object. It’s time consuming and hit the performance too.

2) Avoid Server Trip- It’s always smart to avoid unnecessary server trip. Some of the methods to avoid server side trips are 1) Do client side validation 2) Implement Ajax control for web application so only partial page will be loaded and not the full page.3) use Page.ISPostBack -Use Page.ISPostBack property to ensure that you only perform page initialization logic when a page is loaded the first time and not in response to client post backs.

3) Save View state- Save the view state only when it is must otherwise skip it. When we can avoid to having view state true. 1) Your page does not post back. It just show the data and there is no need to reload it again. 2) you do not handle server control events in your page. 3) If you ignore old data, and if you repopulate the server control each time the page is refreshed

4) Use of Session Variables- When storing the data in session variable make sure you use efficiently. Even though you close the browser but the session variable remain on server for a long time.

5) Use Server.Transfer – whenever you need to redirect to an page within the website you use Server.Transfer and not Response.Redirect. If you need authentication and authorization checks during redirection, use Response.Redirect instead of Server.Transfer

6) Choose the data viewing control- DataGrid control can be a quick and easy way to display data, but it is frequently the most expensive in terms of performance. Rendering the data yourself by generating the appropriate HTML may work in some simple cases, but customization and browser targeting can quickly offset the extra work involved. A Repeater Web server control is a compromise between convenience and performance. It is efficient, customizable, and programmable.

7) Optimize code and Exception handling- Use for loop instead of for each loop. It’s better to write a code which check the condition and make sure you don’t have exception in your programme flow. Because handling the exception and writing it on server will hit the performance.

8) Use DataReader- Use datareader for fast retrieval. If you want to show just read-only data then better to use datareader instated of dataset.

9) Use Paging- It is general idea that people don’t want to see thousands of data. So it is always better to use paging so that it will increase the performance.

10) Explicitly close resource- Always use try /finally and make sure you close and dispose all the connection and any other objects which is not required any more.

11) Disable Tracking and debugging. Always disable tracking and debugging when you deploy the application on production server. Set debug=false in web.config

12) Use store procedures- In most cases you can get an additional performance boost by using compiled stored procedures instead of ad hoc queries.

13) Always make sure you check Page.IsValid before processing your forms when using Validator Controls.

14) When you can, use toString () instead of format (). In most cases, it will provide you with the functionality you need, with much less overhead.

15) Place StyleSheets into the Header

16) Put Scripts to the end of Document

17) Make JavaScript and CSS External -Using external files generally produces faster pages because the JavaScript and CSS files are cached by the browser. Inline JavaScript and CSS increases the HTML document size but reduces the number of HTTP requests.

No comments: