Saturday, February 21, 2015

How to increase the performance of asp.net website?

1. Avoid Inline Code

Avoid inline css styles code, place them in css file.
Avoid inline java-script code, place them in separate java-script file.


2. Placement of JavaScript file

When scripts are defined on top of the page they can take unnecessary time to load. It's better to display the HTML contents of a page firstly and then load any scripting code. So place javascript file reference bottom of page content.

3. Optimize css file

Clean up style sheets and remove unnecessary code from style sheets because it increases the load time. Remove all unnecessary classes from style sheets and try to use a single CSS file. CSS files are normally cached by browsers, so a single and heavy .css file doesn't cause a long wait on each page request.

4. Minify Resources

Since every unnecessary piece of code adds to the size of your page, it’s important that you eliminate extra spaces, line breaks, and indentation in your code so your pages are as lean as possible. You can achieve it by minification so minify your java-script files and css files.

5. Optimize Images

With images, you need to focus on three things: size, format and the src attribute.

Image Size:
  • Crop your images to the correct size. For instance, if your page is 570px wide, resize the image to that width. Don’t just upload a 2000px-wide image and set the width parameter (width=”570”). This slows your page load time and creates a bad user experience.
  • Reduce color depth to the lowest acceptable level.
  • Remove image comments.

Image Format:
  • Do not use BMPs or TIFFs.
  • JPEG is the best option.
  • PNG is also good option but older browsers may not fully support it.
  • GIFs should only be used for small or simple graphics (less than 10×10 pixels, or a color palette of 3 or fewer colors) and for animated images.

Src attribute:
Avoid empty image src codes. In HTML, the code for an image includes this:

<img src=””>

When there’s no source in the quotation marks, the browser makes a request to the directory of the page or to the actual page itself. This can add unnecessary traffic to your servers and even corrupt user data.

6. Http Compression

HTTP compression is used to compress page content from the server end. It compress HTTP requests and responses, so is a great performance improvement

7. Disable possible ViewState

View State allows the page state to be persisted with the client, and it does not require cookies or server memory. View State saves data in a hidden input filed inside a page. Definitely, it is a powerful feature, but the drawback is it increases page size and memory allocation in the server.

So, we should avoid View State where it is not necessary. Especially, in the case of DataGrid controls, we should avoid View State as it loads all the grid data in the page state.

8. Server.Transfer() instead of Response.Redirect()

Search your code for “Response.Redirect” and consider replacing it with Server.Transfer. This does not incur the cost of a new request because it avoids any client-side redirection.

9. Proper use of "Page.IsPostBack"

Make sure it is properly used to avoid repetition code execution. Check that the logic in your page uses the Page.IsPostBack property to reduce redundant processing and avoid unnecessary initialization costs. Use the Page.IsPostBack property to conditionally execute code, depending on whether the page is generated in response to a server control event or whether it is loaded for the first time.

10. More use of client side validation

Validate user input on the client to reduce round trips to the server. This also provides better feedback to the user. For security reasons, ensure that any client-side validation is complimented with the equivalent server-side validation

11. Deploying Production Code in Release Mode

Check your Web.config file and ensure debug is set to false in the <compilation> section and check your .aspx pages to ensure debug is set to false. If debugging is enabled, the compiler does not generate optimized code and pages are not batch compiled. 

12. Proper use of server controls

Evaluate your use of server controls to determine if you can replace them with light weight HTML controls or possibly static text. You might be able to replace a server control under the following conditions:
  • The data being displayed in the control is static, for example, a label.
  • You do not need programmatic access to the control on the server side.
  • The control is displaying read-only data.
  • The control is not needed during post back processing.

13. Manage Strings Efficiently
  • Use += for concatenating stringsIdentify places in your code where you perform string concatenation by using the += operator. If the number of appends is unknown, or you are appending an unknown size of data, consider using the StringBuilder class instead.
  • Use strString=string.Empty instead of strString="" or strString=null

14. Proper use of caching
  • Check your pages that use the output cache to ensure that the number of variations has a limit. Too many variations of an output cached page can cause an increase in memory usage. You can identify pages that use the output cache by searching for the string “OutputCache”.
  • When reviewing your pages, start by asking yourself if the whole page can be cached. If the whole page cannot be cached, can portions of it be cached? Consider using the output cache even if the data is not static. If your content does not need to be delivered in near real-time, consider output caching. 
  • Using the output cache to cache either the entire page or portions of the page can significantly improve performance.
  • Identify application-side data that is static or infrequently updated. This type of data is a great candidate for storing in the cache like “Contact us” and “About us” pages.
  • Checking for null before accessing the cached item as shown in the following code fragment.

15. div instead of table

Replace table with div wherever possible as it increase performance. Read my another blog for div vs table.

No comments:

Post a Comment