Introduction to Bundling and Minification in Asp.net/MVC
Bundling & Minification is a Website Performance Optimization approach provided with asp.net 4.5 version .
It can be added to asp.net 4.0 version too with explicit installation from Nugget Package Manager tool.
Bundling & Minification are two techniques we can use in ASP.NET to improve website load time.
These techniques improve load time by reducing the number of requests to the server and reducing the size of requested resources (CSS, Java Script).
Most of the current major browsers limit the number of simultaneous connections per each hostname to six.
That means that while six requests are being processed, additional requests for assets on a host will be queued by the browser.
It can be added to asp.net 4.0 version too with explicit installation from Nugget Package Manager tool.
Bundling & Minification are two techniques we can use in ASP.NET to improve website load time.
These techniques improve load time by reducing the number of requests to the server and reducing the size of requested resources (CSS, Java Script).
Most of the current major browsers limit the number of simultaneous connections per each hostname to six.
That means that while six requests are being processed, additional requests for assets on a host will be queued by the browser.
Bundling:
As mentioned Bundling is a new feature in ASP.NET 4.5 and that can be implemented in ASP.NET 4.0 version as well with the help of System.Web.Optimization framework and that makes it easy to combine or bundle multiple files into a single file.You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load performance.
Minification:
Consider the following JavaScript function.
After minification it removes unwanted comments and shortens the variable names, the function is reduced to the following:
Bundling and Minification Impact in Web Sites
The following table shows several important differences between listing all the assets individually and using bundling and minification (B/M) in the sample program.
Following table is for representation and shows the impact of B&M in Web Applications.
Using B/M | Without B/M | Change | |
---|---|---|---|
File Requests | 9 | 34 | 256% |
KB Sent | 3.36 | 11.92 | 266% |
KB Received | 388.51 | 530 | 36% |
Load Time | 510 MS | 780 MS | 53% |
Bundling and Minification Implementation:
It is very easy to implement B&M in our web applications.
This can be enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web.config file.
In the following XML, disable the bundling and minification by setting debug to true.
This can be enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web.config file.
In the following XML, disable the bundling and minification by setting debug to true.
We can override the Web.config setting with the EnableOptimizations property on the BundleTable class too.
Note: Please note that Unless EnableOptimizations is true or the debug attribute in the compilation Element in the Web.config file is set to false, files will not be bundled or minified.
Additionally, the .min version of files will not be used, the full debug versions will be selected.EnableOptimizations overrides the debug attribute in the compilation Element in the Web.config file.
If you are using .net 4.0 versions then we need to install the System.Web.Optimizatin framework from Nugget Package Manager Tool. It Installs Other Dependent DLL’s like WebGreese.DLL.
Practical Approach to Implement Bundling and Minification:
In my case I have created a separate file for listing all resources (Including CSS, JS) in XML format.Take a look at the sample of xml file below.
This Class reads xml file and adds js/css files to bundle using bundle classes referenced from Optimization framework.
The following code enables bundling and minification and overrides any setting in the Web.config file.
Bundling and Minification will be registered from Global.asax file.
In order to enable bundling and minifaction we have to register functionality in global.asax file.
In Global.asax Applicatin_Start() method.Create the object of helper class which we created for bundling and minification.
Comments
Post a Comment