Steve Souders began to describe Web Performance Optimization 10 years ago. WPO is the field of knowledge about increasing the speed in which web pages are downloaded and displayed on the user’s web browser. He wrote and contributed to many books (High Performance Web Sites, Even Faster Web Sites, Web Performance Daybook V2) to explain to us his best practices for performance along with the research and real-world results behind them.
One of the most important rules is to Combine & Minify resources. Bundling combines multiples files into a single file whereas Minification is the process of removing all unnecessary characters from source code without changing its functionality.
With the latest HTML5 specification and the emergence of JS frontend frameworks like JQuery and more recently AngularJS, JavaScript has never been so used and so popular. We can now create scalable, maintainable applications, unified under a single language: JavaScript !
As we’re all good web citizens, all our resources (JS/CSS) are bundled and minified on production. This is sometimes where things start going bad. Have you ever tried to debug JavaScript on production? The primary drawback of this optimization is that it makes debugging your JavaScript code a nightmare, since the browser’s developer tools will show you code that is virtually unreadable.
For example, a production JavaScript may looks like this: 14K characters on the same line.
It’s impossible to debug the previous script because browsers can’t set breakpoint at character level (only at line-level).
Helpfully, some of the browsers have an option in developers tools to un-minify partially a JavaScript file. This option is called “pretty print” and the icon is like {}
.
Chrome
Internet Explorer
,
In Firefox (since December 2013)
Here is the result after pretty print.
It’s fairly better and you have nearly the same debugging experience as that for dev scripts. Sometimes, it’s not enough because JavaScript minification tools rename local functions & variables. It’s common to see a()
, b()
, c()
in a minified script. A good indentation won’t change this.
Fortunately, Source Maps provide a way of mapping the lines & columns of the production source code (bundled & minified), back to their original locations in the corresponding uncompressed source files. This feature is supported by all modern browsers.
An additional file is generated during the minification process and is added at the top of the optimized file.
Source maps are easily generated by grunt-contrib-uglify or Closure Compiler. Unfortunately, it’s still not supported by Microsoft ASP.NET Web Optimization Framework. For sure, this is something that needs to be done. Web Essentials also offer this feature.
It’s a nice tip for every web developer but the good question may be: Why do you have to debug production code?
CodeProject