8 Tips to Increase Your ASP.NET Website Speed

This article suggests a few tips to improve the performance of an ASP.NET application. There are many more things which may ensure a better performance and faster response time for a web application. I am discussing only a few of the best practices that will help you avoid some unwanted performance hitters from your application. So, you will have a more lightweight application which runs quicker and gives a better response time.

#1. Use Caching

Caching is a good technique to improve your application’s performance. If your application has infrequent data changes or the application has more static content of web page, you can use caching. If your application does not mandate near real-time content to be delivered, consider using output caching. But, the data won’t be the latest for the duration mentioned while enabling caching.

When using caching, what it does is – it stores the output of the page. So, the subsequent requests for the page are loaded immediately by serving this output instead of generating the output. This output is served for the certain period mentioned while enabling caching.

You can cache entire page or fragments of pages or controls depending on the type of static data you have.

#2. Disable View State if Possible

View State is a technique used by an ASP.NET to persist changes to the state of a web form across postbacks to retain the current data for a web application which is otherwise stateless. But View State increases the load of the page both when requested and served. There is also an additional overhead of serializing and de-serializing view state data as it is posted back. View State increases the memory allocations on the server as well.

We can disable the View State of the pages where there is no postback required. This is applicable for controls as well. By default, View State is turned on for all pages and controls. So, turn it off for a page or a control wherever it is not required.

  • Disable view state for a single page,
    <%@ Page EnableViewState=”false” %>
  • Disable view state for a single control on a page,
    Set EnableViewState = “false”

#3. Set debug=false

When you create the application, debug attribute will be set to “true” by default since it is very useful during development. But, always set debug=”false” before deployment. This is a very small thing you need to do but will have a greater impact on the application performance.

#4. Avoid Unnecessary Round Trips to Server

Round trips to server significantly affect performance. This is because the requests and responses are created and transferred to server and client. It also takes some time for the server to do the processing and object creation before the response is sent back. Adding to this, sometimes other factors, such as a server is busy with too many requests, network latency etc., can further affect the speed. So, keep round trips to an absolute minimum. How it can be done in each case may depend on your application. A few examples are:

  • Use Ajax UI whenever possible
  • Do user input validation on the client side using JavaScript
  • Avoid unnecessary database hits to load the unchanged content in the database
  • Use IsPostBack property

#5. Use Specific CSS and Script files

Using large CSS files that are used for the entire site in multiple pages will increase the loading time of the page thus leading to a performance hit. It can be split and stored in different files thus loading only what is required for each page. It will minimize the loading time of the pages.

For example,

contactus.css can be used for ContactUs.aspx and home.css can be used for Home.aspx, The same way you can split your JavaScript files as well.

#6. Use Paging

Take advantage of paging’s simplicity in .net. Loading a large number of records into the server data controls like GridView, DataGrid, and ListView etc. will take a lot of time for the page to load. But, if we show only small subsets of data at a time, the page will load faster. On click of Next button, Previous button or on the page number, you can load the next page of data.

#7. Use String builder to concatenate strings

Use String instead of StringBuilder when you must modify or concatenate a string several times. A string is immutable while StringBuilder is mutable. You cannot change immutable objects after it was created. Any operation that intended to change it, will return a new instance.

For example, the following code creates 2003 instances of string object.

On the otherhand, the code with StringBuilder puts much less stress on memory allocator as it is creating only one instance and modifying it.

#8. Use Stored Procedures

Use Stored Procedures to avoid multiple database hits and get many things done on a single hit. Also, there is a slight performance benefit for the stored procedure over queries as Store Procedures are compiled and stored in the SQL database server, unlike the queries which require compilation. If you are using really long queries and if you change it to SP, the advantage will be more obvious. Because for sending really long queries over the network, more bandwidth need to be used unlike and SP will take only a short line.

Steve
Latest posts by Steve (see all)