ASP.Net Site Performance: Improving JavaScript Loading ASP.Net Site Performance: Improving JavaScript Loading

ASP.Net Site Performance: Improving JavaScript Loading


 

aspnet-site-performance-improving-javascript-loading-img-0

Simple and proven techniques to quickly speed up your ASP.NET website

  • Speed up your ASP.NET website by identifying performance bottlenecks that hold back your site’s performance and fixing them
  • Tips and tricks for writing faster code and pinpointing those areas in the code that matter most, thus saving time and energy
  • Drastically reduce page load times
  • Configure and improve compression – the single most important way to improve your site’s performance
  • Written in a simple problem-solving manner – with a practical hands-on approach and just the right amount of theory you need to make sense of it all

 

One approach to improving page performance is to shift functionality from the server to the browser. Instead of calculating a result or validating a form in C# on the server, you use JavaScript code on the browser. A drawback of this approach is that it involves physically moving code from the server to the browser. Because JavaScript is not compiled, it can be quite bulky. This can affect page load times, especially if you use large JavaScript libraries. You’re effectively trading off increased page load times against faster response times after the page has loaded.

In this article by Matt Perdeck, author of ASP.NET Site Performance Secret, you’ll see how to reduce the impact on page load times by the need to load JavaScript files. It shows:

  • How JavaScript files can block rendering of the page while they are being loaded and executed
  • How to load JavaScript in parallel with other resources
  • How to load JavaScript more quickly

(For more resources on ASP.Net, see here.)

JavaScript files are static files, just as images and CSS files. However, unlike images, when a JavaScript file is loaded or executed using a

The second approach is to simply spend less time loading the same JavaScript, so that visitors spend less time waiting for the page to render. There are a number of ways to achieve just that:

  • Techniques used with images, such as caching and parallel download
  • Free Content Delivery Networks
  • GZIP compression
  • Minification
  • Combining or breaking up JavaScript files
  • Removing unused code

Techniques used with images

JavaScript files are static files, just like images and CSS files. This means that many techniques that apply to images apply to JavaScript files as well, including the use of cookie-free domains, caching, and boosting parallel loading.

Free Content Delivery Networks

Serving static files from a Content Delivery Network (CDN) can greatly reduce download times, by serving the files from a server that is close to the visitor. A CDN also saves you bandwidth because the files are no longer served from your own server.

A number of companies now serve popular JavaScript libraries from their CDNs for free. Here are their details:

In ASP.NET 4.0 and later, you can get the ScriptManager control to load the ASP. NET AJAX script files from the Microsoft AJAX CDN instead of your web server, by setting the EnableCdn property to true:

  EnableCdn="true" runat="server" />

One issue with loading libraries from a CDN is that it creates another point of failure—if the CDN goes down, your site is crippled.

GZIP compression

IIS has the ability to compress content sent to the browser, including JavaScript and CSS files.

Compression can make a dramatic difference to a JavaScript file as it goes over the wire from the server to the browser. Take for example the production version of the jQuery library:

 

Uncompressed Compressed
jQuery library 78 KB 26 KB

 

Compression for static files is enabled by default in IIS 7. This immediately benefits CSS files. It should also immediately benefit JavaScript files, but it doesn't because of a quirk in the default configuration of IIS 7.

Not all static files benefit from compression; for example JPEG, PNG, and GIF files are already inherently compressed because of their format. To cater to this, the IIS 7 configuration file applicationHost.config contains a list of mime types that get compressed when static compression is enabled:






To allow IIS to figure out what mime type a particular file has, applicationHost.config also contains default mappings from file extensions to mime types, including this one:


...
mimeType="application/x-javascript" />
...

If you look closely, you'll see that the .js extension is mapped by default to a mime type that isn't in the list of mime types to be compressed when static file compression is enabled.

The easiest way to solve this is to modify your site's web.config, so that it maps the extension .js to mime type text/javascript. This matches text/* in the list of mime types to be compressed. So, IIS 7 will now compress JavaScript files with the extension .js (folder Minify in the downloaded code bundle):






Keep in mind that IIS 7 only applies static compression to files that are "frequently" requested. This means that the first time you request a file, it won't be compressed! Refresh the page a couple of times and compression will kick in.