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.
- 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.
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.
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
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
6. CDN (Content Delivery Network)
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
scripts 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
scripts, so they can be rendered. In such cases, you could move those scripts further up the page.
There are some other ways:
<script src="some.js" defer>
defer attribute, you can specify the
script not to run until the page has been fully loaded.
<script src="some.js" async>
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.