Introduction
In this tip, we will look at various aspects of improving the performance of ASP.NET web applications.
Performance is an important aspect of the modern day web application development. Here are some tips that you can consider while making a better performing website.
1. Upgrade Your ASP.NET Framework
Check your .NET framework. If you can upgrade your site to use .NET 4.5, then it has some great performance optimizations. .NET 4.5 has a new Garbage Collector which can handle large heap sizes (i.e., tens of gigabytes). Some other improvements are Multi-core JIT compilation improvements, and ASP.NET App Suspension. These optimizations do not require code changes.
2. Caching
- Use output caching – Use output caching for regularly used views or pages which have no dynamic updates. The easiest way to implement cache on MVC view is to add an
[OutputCache]
attribute to either an individual controller action or an entire controller class. Here is a controller action Index()
that will be cached for 15 seconds.
[OutputCache(Duration = 15, VaryByParam = "None")]
public ActionResult Index(string Id)
{
}
- Use Data caching - Reduces the database or the disk I/O by caching the regularly used data to in-memory cache. This avoids repeated queries for data, and it can improve performance and scalability. In addition, caching makes data available when the data source is temporarily unavailable. The .NET Framework provides classes that enable you to use caching facilities in ASP.NET applications. These classes are defined in the System.Runtime.Caching namespace.
3. Always keep CSS and JavaScript External
Never add any JavaScript or inline style information within the views. That would regenerate the view each time and you would miss out on the benefits of the Caching. Hence always keep JS and CSS as separate files and add them as links in the view.
4. File Compression
There are often requests to the web server with lot of static content. These contents can be compressed thereby reducing the bandwidth on requests. The following setting is only available in II7 and later.
<configuration>
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
</configuration>
The urlCompression
name sounds strange but it is not really the compressing of URLs. It means compressing or gzipping the content that is sent to the browser. By setting to true
/enabling, you can gzip content sent to the browser while saving lots of bandwidth.
5. Bundling and Minification
The custom CSS files and the JavaScript files should be bundled into a single large file (reduces the number of HTTP requests) and also minified (reduces the size of the data transferred over the wire).
6. CDN (Content Delivery Network)
All the 3rd party JavaScript files such as JQuery, Knockout should always use the CDN instead of the web application server. CDN Servers are dedicated to deliver the static content and is always faster than your own host. There is a very high probability that the client (browser) would have already cached the JavaScript as part of other web application since most of them use the same CDN URL.
7. Control Image Requests
There are couple of ways to do this.
- Image sprite - With image sprite, you can combine multiple different images into a single large image. Then use CSS to reposition those images within the site.
- Base64 Data URIs - With this option, you would never make any requests to the server to obtain any images.
8. Script Rendering Order
Move the script tags <script>
to the very bottom of the page. The reason this is important is because during the rendering, when the browser comes across a <script>
tag, it stops to process the script and then moves ahead. If you put the script
tags at the bottom of the page, the page/HTML will render faster and the script
s can execute after the DOM elements have loaded. Sometimes moving the script
to the bottom of the page is not possible as some DOM elements or CSS may depend on these script
s, so they can be rendered. In such cases, you could move those scripts further up the page.
There are some other ways:
defer
attribute
<script src="some.js" defer>
</script>
Using the defer
attribute, you can specify the script
not to run until the page has been fully loaded.
async
attribute
<script src="some.js" async>
</script>
Using the async
tag, the scripts will be run asynchronously, as soon as it is available.
9. Removing Default HTTP Modules in ASP.NET
ASP.NET has many http modules waiting for request to be processed and would go through the entire pipeline even if it’s not configured for your application.
All the default modules will be added in the machine.config
place in“$WINDOWS$\Microsoft.NET\Framework\$VERSION$\CONFIG” directory. One could improve the performance by removing those modules which you wouldn’t require.
10. Compile in Release Mode
Always set the build configuration to release mode for the website.
In this tip, we looked at some of the approaches that you can take to make optimized and better performing web sites. This included, reducing number of requests to the server, changes to the .NET framework, and compressing techniques.