Techniques Behind Modern Web
20 Feb
For a heavy JavaScript web application, one of the most important things you should do before making it public is to obfuscate and compress JavaScript and CSS. The reason to do that is obvious: your app will be faster and safer.
When the web code is not optimized, it makes your visitors waste bandwidth and wait longer for the page to load. Besides, it makes your hard work easy to steal and reverse engineer.
I’ve tried some solutions for obfuscating and compressing JavaScript/CSS from both commercial vendor and open-source community. Here are my 2 cents about the tools.
This was the first tool I used to compress JavaScript. Claimed as the “safe” tool, it does very little in term of obfuscating codes but just replace variable, argument names with numbers as example below.
Original codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function MyClass(){ this.foo = function(argument1, argument2){ var addedArgs = parseInt(argument1)+parseInt(argument2); return addedArgs; } var anonymousInnerFunction = function(){ // do stuff here! } } function MyFunc(){ // this is a top-level function } // we've got multiple lines of whitespace here |
Run the tool with command line:
1 | java -jar custom_rhino.jar -c infile.js > outfile.js 2>&1 |
And the result looks like:
1 2 3 4 5 6 7 8 9 10 | function MyClass(){ this.foo=function(_1,_2){ var _3=parseInt(_1)+parseInt(_2); return _3; }; var _4=function(){ }; } function MyFunc(){ } |
You’ll get about 50% reduction in code length — not too bad. I like Dojo ShrinkSafe because I’ve never run into trouble with codes shrunk by the tool and in most of cases I can still debug JavaScript codes after compressed (but some smart people can read your codes too).
Read more and download Dojo ShrinkSafe here or try online tool here.
YUI Compressor is a part of YUI library and in active development now. The latest version (2.3.5) was just released a few weeks ago.
Better than Dojo SS, YUI Compressor supports both JavaScript and CSS compression and I’m quite satisfied with the compression ratio (about 10% smaller than Dojo SS or JSMin).
Using this tool is much similar to Dojo SS - it’s a Java command-line tool and I often write Ant scripts (see the excerpt below) in my projects to automate all concatenating/compressing tasks.
1 2 3 4 5 6 7 8 9 10 11 | <!--– Create feedready-all.js –--> <concat destfile="${basedir}/scripts/feedready-all.js" fixlastline="”yes”" append="no"> <filelist dir="${basedir}/scripts" files="fr_common.js, fr_feedsnap.js, fr_mod_feedreader.js, fr_mod_save.js, fr_viewer.js"/> <!--– Minify feedready-all.js into feedready-min.js–--> <java fork='true' jar="C:\yuicompressor-2.2.4\build\yuicompressor-2.2.4.jar"> <arg line="-o"></arg> </java> </concat> |
I think this tool is the best one among other open-source solutions like Douglas Crockford’s JSMIN, Dean Edwards’ Packer and Dojo SS mentioned above.
Please go to official home page of YUI Compression for detail usage instruction.
Jasob Obfuscator offers a desktop GUI (for Windows only) that helps you easily creating “Project” for obfuscation.

This is a commercial tool used by many companies including Cisco Systems, Autodesk, Saxo Bank A/S, Swisscom AG/ Innovations etc. so you can trust the reliability of the product.
The killer feature of Jasob Obfuscator is capable of reading JavaScript/CSS code embedded in HTML, ASP, JSP, PHP, XML… pages and do obfuscation. It can also modify server-side code to match up with changes made by the obfuscation.
I haven’t used this tool in a real project (I don’t want the above features because I’ve hardly embedded long JavaScript code into server-side pages) but my trial to compress a script file for SHA-1 hash generator resulted in a slight bigger file in comparison with the one compressed by YUI Compressor (2,945 over 2,596 bytes). However, I suppose it’s not too large different.
Find more about Jasob JS/CSS Obfuscator here.
Personally, I prefer open-source solutions and find YUI Compressor is the best one in both terms of compression ratio and features it provides. It’s especially convenient when used in Java-based development environment (like Eclipse, NetBeans) for I can create automated tasks with Ant.
However you may want commercial solution like Jasob Obfuscator if your project contains many JavaScript codes scattered in HTML or server pages.
I’ve tried to compress a script file (for SHA-1 hash) with original size of 5,752 bytes using above tools, and here is result for your consideration.
| File | Size (bytes) | Compression ratio |
| Original (sha1.js) | 5,752 | 0% |
| Dojo SS compressed | 2,950 | 48.7% |
| YUI Compressor compressed | 2,596 | 54.9% |
| Jasob Obfuscator compressed |
Of course, compression efficiency is just one factor you should consider when selecting a tool for your project.
Please download compressed script files and a test page to validate all scripts working properly after being compressed.
Update: Following guides by Jasob Support, I did a new test with the tool: generating new (shorter) names for all variables and private functions. Result is the obfuscated file of only 2,310 bytes, nearly 60% reduction in size, that makes Jasob WINNER!
Free Website Magazine: Know more than your competitors with Website Magazine
Don't forget to subscribe
so you don't miss out on future posts!
Lack of GUI shouldn’t be considered a con, especially when these utilities will only be run when exe’ing your RC build or Final build ant script. If you run these against development, you destroy any ability to maintain the code. The real con here is price.
Well, is lacking of GUI a feature?
Another option is to use Include. We’ve been using it to make compressing and including scripts ridiculously easy. It determines which files to compress at runtime and automatically compresses them using Dean Edwards’ Packer. You’ll never have to write another one-time server-side compression script again.
Check it out at http://javascriptmvc.com/include. I hope you find it as useful as we have.
@Brian: Your project looks very impressive but I still concern about Dean Edwards’ Packer for potential security risk, JS safety and execution performance.
How do you think?
@Jimmy: Very valid concerns. However, we turn off the use of eval in Packer. The compression is due to variable name shrinking, whitespace reduction, and combining all your separate scripts into one. So the security and safety concerns aren’t an issue. I’m not totally sure what you mean by execution performance. As far as compression rate, Packer is up there with the best compressors.
If you’d prefer YUICompressor, we do support a solution for that also. You use Include to produce the ordered list of files for compression, put this in a text file on your server, and run the Ruby compression script we’ve made, which uses YUI Compressor by default. Since the YUI Compressor is written in Java, it doesn’t work in the client like Packer does. You can substitute YUI Compressor for any compressor you fancy. There’s more info on that, as well as a download link, at the main page: http://javascriptmvc.com/include.
Good questions! If you have any more, let me know.
Thanks for your insightful explanation. I’ll try to integrate “include” into one of my projects for in-depth knowledge of how it works. Probably, I’ll have a review on it soon.
Jasob has the BEST compression efficiency. The size of the obfuscated file (sha1.js) of 2,945 bytes is WRONG. The correct file size after obfuscating it with Jasob is 2.243 bytes which clearly makes Jasob a winner in this comparison. To achieve best compression ratio, names from both name lists should be first marked for obfuscation and then new names should be generated from the main menu (Obfuscate -> Generate New Names) or obfuscation process should be started (Obfuscate -> Obfuscate). Please update comparison table with correct obfuscated file size (2.243 bytes) for Jasob obfuscator.
Thanks for pointing out the functionality. I did a new test: generating new names for all variables and private functions. My result is the obfuscated file size of 2,310 bytes. Jasob is WINNER!
Thank you for updating test results. In the given example (index.html and sha1.js files) you can simply mark ALL names for obfuscation which would give you even better results (2,243 bytes for the obfuscated sha1.js file). Please remove or modify the following in your Jasob review as it is simply not correct: “…but my trial to compress a script file for SHA-1 hash generator resulted in a slight bigger file in comparison with the one compressed by YUI Compressor (2,945 over 2,596 bytes). However, I suppose it’s not too large different.” and in the Cons section “…NOT the best compression ratio”.
I don’t think selecting everything for obfuscation is wise choice. You may want keep public function names as original, especially in case of SHA-1 lib. Of course, I know that if we use Jasob for a project, all other pages will “know” obfuscated function names so we can choose to obfuscate all. Correct me if I’m wrong.
That depends on your project and your preferences. By obfuscating all names (or as much as you can) you will not only make obfuscated file size smaller but overall obfuscation will also be better. Sure, if you have JavaScript library that you want to distribute to other developers then you may choose not to obfuscate certain public functions or global variables which at the same time doesn’t mean that end user (developer) cannot obfuscate those public functions and variables as well. Regarding the summary in your review, part of the sentence “…and find YUI Compressor is the best one in both terms of compression ratio” is not correct. Thanks.
The point is that YUICompressor can automatically detect what to obfuscate while Jasob requires you to do it manually. That’s why the results could be the worst (2,945 bytes) or the most-optimized (of 2,243 bytes - but not the best for public variable and function names would be lost.)
That’s not true, Dave. In Jasob you can use various criteria to automatically mark or unmark certain names for obfuscation, including automatically unmarking global names (variables and functions) for obfuscation. There’s no way an application could automatically detect which global variables or functions can be obfuscated and which cannot because there’s simply no such information inside a single .js file as you are saying YUICompressor is doing. What do you mean by “worst results”? We don’t know how the author of this review initially came up with obfuscated file size of 2,945 bytes. Worst results could be without obfuscating any name and without removing comments and whitespaces. At the end, any given .js file will be part of an application and then this distinction between pubic and private variable and function names would be irrelevant in the terms of the obfuscation process which will give you the opportunity to obfuscate all those public names as well and with Jasob you can do that.
Very good article. It would be great if the author can include dean edwards packer also as part of the benchmark.
Obfuscating JavaScript and CSS: Open Source vs Commercial Solutions | Just Talk About Web…
Obfuscating JavaScript and CSS: Open Source vs Commercial Solutions | Just Talk About Web…
[...] tagged javascriptOwn a Wordpress blog? Make monetization easier with the WP Affiliate Pro plugin. Obfuscating JavaScript and CSS: Open Source vs Com… saved by 3 others ifon1stdate bookmarked on 02/22/08 | [...]
[...] Mar A few weeks ago, I posted a comparison of Dojo ShrinkSafe, YUI Compressor and Jasob Obfuscator. Then, I received comment from Brian introducing an interesting project: Include; some day later it [...]