I am searching for a way to compress javascript for the iphone but don't want to use a lot of cpu time on the small an rather slow device.

+9  A: 

Use the YUI Compressor

+5  A: 

Use JSMin and avoid packer which is really more CPU consuming and slower to "deflate"

Cool there even is a PHP Version so I can automate it
+1  A: 

I believe Safari on the iPhone supports gzip output so you could use something like mod_deflate. I've had the best results using this method. Quite a bit of the JavaScript compression stuff out there is absolute garbage and takes longer to decompress than it does to download the larger file. JSMin looks pretty good, though.

+3  A: 

I love ShrinkSafe. It interprets your code in Rhino, then it returns compressed code. Because it's operating on real interpreted code (instead of complex string evaluations) it will never munge code or fail to find differences between public and private variables.

It's a tool of excellent quality.


Making sure your webserver properly serves stuff gzipped/deflated when the client supports it is usually more effective than minifying the program code itself. Of course, using both tends to give even smaller sizes.

+3  A: 

We've used js_compactor and JavaScriptLint to "compile" and compress our JavaScript in our automated build process. A further build step would take the compress JavaScript and combine related files into a single package. The performance boost was significant, but be aware that you are away trading the ability to debug.

Reducing the number of files transmitted to the client will gives you a big performance boost when there are more than a few files. Typically, browsers will only open 2 connections to a single server at a time, so even if you are transmitting compressed and minimized files the browser spends a significant amount of overhead checking its cache. yslow helped us identify why pages were taking a long time to load and help us focus our optimization efforts. We instrumented our environment to either use the raw files or the minimized and compressed versions.

John Naegle
+1  A: 

You can try different tools at The JavaScript CompressorRater. All tools except packer have no impact on how fast the javascript executes as far as I know - they only removes whitespaces, renames variables and such.

I myself considers YUI Compressor to be the best one.

It's always useful to validate the code in JSLint first to be sure that the compressor understands it correctly.


I just went through this little dance in the last few days. We tried using packer, but found that our packed JavaScript was taking over 2 seconds to execute (not to mention blocking other downloads). Based on this article we've switched to YUI Compressor. Not only are our gzipped file sizes smaller, execution times are down under 300ms.

Andrew Hedges