JSMin: Javascript Compression

While at The Ajax Experience in October, I attended a presentation who spoke of the 3 C's (Combine, Compress, Cache) for Ajax development. In the Compress section I was introduced to the beauty of JSMin!

What is it? Well, shut up and I'll tell you.

Quoting Douglas Crockford (the creator of JSMin):

JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files. It typically reduces filesize by half, resulting in faster downloads. It also encourages a more expressive programming style because it eliminates the download cost of clean, literate self-documentation.

As I've been creating more complex Javascript applications, the file size has been increasing (although the size is greatly reduced, thanks to jQuery). And, as a careful programmer should, I place comments all over my Javascript code so I don't draw too many blanks while updating/debugging...well, those comments tend to bloat the file size, as does the whitespace. The stripping of those elements alone has dropped the file-size of a number of my scripts by 40-50%! Thats huge. What previously was an 8K file goes down to 4K. Awesome.

If you're curious what JSMin does to your code, here's an example that Douglas gives:
Before

JavaScript:
  1. // is.js
  2.  
  3. // (c) 2001 Douglas Crockford
  4. // 2001 June 3
  5.  
  6.  
  7. // is
  8.  
  9. // The -is- object is used to identify the browser.  Every browser edition
  10. // identifies itself, but there is no standard way of doing it, and some of
  11. // the identification is deceptive. This is because the authors of web
  12. // browsers are liars. For example, Microsoft's IE browsers claim to be
  13. // Mozilla 4. Netscape 6 claims to be version 5.
  14.  
  15. var is = {
  16.     ie:      navigator.appName == 'Microsoft Internet Explorer',
  17.     java:    navigator.javaEnabled(),
  18.     ns:      navigator.appName == 'Netscape',
  19.     ua:      navigator.userAgent.toLowerCase(),
  20.     version: parseFloat(navigator.appVersion.substr(21)) ||
  21.              parseFloat(navigator.appVersion),
  22.     win:     navigator.platform == 'Win32'
  23. }
  24. is.mac = is.ua.indexOf('mac')>= 0;
  25. if (is.ua.indexOf('opera')>= 0) {
  26.     is.ie = is.ns = false;
  27.     is.opera = true;
  28. }
  29. if (is.ua.indexOf('gecko')>= 0) {
  30.     is.ie = is.ns = false;
  31.     is.gecko = true;
  32. }

After

JavaScript:
  1. var is={ie:navigator.appName=='Microsoft Internet Explorer',java:navigator.javaEnabled(),ns:navigator.appName=='Netscape',ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=='Win32'}
  2. is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
  3. if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}

As you can see...the result is not easily read so you'll want to keep your original script around in the event that editing is needed. Super cool. So how do you get it? Well...there's a number of languages that JSMin logic has been ported to: zip file containing
an MS-DOS.exe file
, or you can get the C source code and build it yourself. Now in C# and Java and JavaScript and Perl and PHP and Python and Ruby.

If you haven't thought about Javascript compression yet, you might want to start. Try it out...you'll be happy you did. Oh, and if you are curious: the Javascript implementation of JSMin is my favorite as it gives some excellent feedback and comment options on compression.

Discuss This Article


5 Responses to “JSMin: Javascript Compression”

  1. AvatarWandering Pig Effer

    I use this tool for JS compression - strips comments and whitespace.

    http://alex.dojotoolkit.org/shrinksafe/

    Reply to this comment.
    1
  2. AvatarMatt
    Author Comment

    Nice. Thats one that was discussed at the Ajax Experience as well…I’ll give it a look-see.

    Reply to this comment.
    2
  3. AvatarJeff

    So can any of these shrinkers be included in an ant build script?

    Reply to this comment.
    3
  4. AvatarMatt Haynes

    Apache Ant task that interfaces with Douglas Crockford’s JSMin program.

    http://code.google.com/p/jsmin-ant-task/

    Reply to this comment.
    4
  5. AvatarFilip

    This method is intended to produce minified js files and not compressed files like gz or zip, also possible and very useful for bandwidth reduction.

    Reply to this comment.
    5

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview:

 (938) - how to use jsmin (47) - javascript Compress (47) - js compress (43) - jsmin (26) - JSMin.exe (25) - js compression (23) - jsmin usage (21) - php compress javascript (17) - javascript compress php (10) - jsmin example (10) - compression js (10) - php javascript compress (9) - download jsmin (7) - compress javascript php (7) - c# compress javascript (7) - use jsmin (6) - javascript compresser (6) - JSMin download (6) - javascript compress download (6) - javascript compress text (6) - javascript zip compression (5) - jsmin compression (5) - javascript compress zip (5) - how to run jsmin (5) - compress javascript mac (5) - JsMin php4 (5) - microsoft ajax javascript compress (5) - jsmin how to (4) - jsmin ant (4) - Javascript compress c# (4) - jsmin jquery (4) - php compress js (4) - using jsmin (4) - php js compress (4) - python javascript compression (3) - jsmin tool (3) - jsmin dos (3) - javascript zip compress (3) - python compress javascript (3) - compress javascript with php (3) - use jsmin.exe (3) - javascript compression zip (3) - download jsmin.exe (3) - compress javascript c# (3) - javascript compression c# (3) - ant javascript (3) - jsmin.exe usage (3) - jsmin exe (3) - jquery (3) - compressing javascript php (3) - google javascript compress (3) - jsmin compress (3) - javascript compress ant (3) - jquery jsmin (3) - jsmin hoe to use (2) - jsmin bug (2) - js compress python (2) - js compress php (2) - jsmin free download (2) - how to use jsmin.exe (2) - apache javacsript compression (2) - zip compress javascript (2) - compressor jquery jsmin (2) - javascript compress python (2) - minify php4 (2) - javascript compress with php (2) - compress js to jsmin (2) - JS minify compress (2) - JS Compression Comparison (2) - zip compression javascript (2) - JSmin PHP 4 (2) - jsmin use (2) - how do you use jsmin (2) - php jsmin bug (2) - combine javascript ant script (2) - I want to use jsmin (2) - compress javascript in php (2) - ant combine files (2) - compress javascript google (2) - javascript apache combine (2) - JSMin internet explorer (2) - text compression javascript (2) - jsmin perl example (2) - JSmin perl (2) - c# jsmin (2) - javascript compress php whitespace (2) - c# javascript compression (2) - c# javascript compress (2) - jsmin javascript (2) - jsmin how to use (2) - ant compress javascript (2) - js compression c# (2) - ant jsmin (2) - js compression code (1) - javascript compression php code (1) - java script compresser (1) - javascript compress and java (1) - compress js perl (1) - jsmin for php4 (1) - jsmin C# google code (1) - javascript minification ant filter (1) - javascript compression number (1) - jsmin c example (1) - ant zip compression (1) - jsmin.exe how to work (1) - compress js min (1) - combine javascript perl (1) - compress ant javascript (1) - ant compress (1) - jsmin, usage (1) - php javascript minification php 4 NOT jsmin (1) - example of using jsmin (1) - compress js files in python (1) - ant javascript combine (1) - C# javascript minify (1) - jsmin Javascript Compression download (1) - javascript compress apache ant (1) - python jsmin and combine (1) - php compress apache ant (1) - c@ js compression (1) - Javascript compresse (1) - wordpress compressing js (1) - apache combine javascript (1) - jsmin on mac (1) - jsmin implementation (1) - ant task javascript combine (1) - compress.php js compress (1) - jsmin.exe commands (1) - .js compress (1) - compression.js (1) - javascript compress cache filter (1) - js compression php (1) - javascript compressor ANT (1) - compress js php4 (1) - c# combine javascript (1) - jsmin how to use msdos (1) - how to use jsmin MS DOS exe (1) - white space compression js (1) - jsmin online (1) - compress javascript plugin wordpress (1) - JSMin internet explorer bug (1) - run jsmin online (1) - javascript build script compression (1) - compress javascript with python (1) - jsmin.exe latest version download (1) - Using MS-Dos jsmin.exe (1) - jquery compress js (1) - minify MicrosoftAjax .js (1) - combine js files ant (1) -