Question

I am searching for a way to compress JavaScript code for the iPhone. Is there a way to avoid using a lot of CPU time on the small and rather slow device?

Was it helpful?

Solution

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

OTHER TIPS

Use the YUI Compressor

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.

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.

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.

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.

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.

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 under 300 ms.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top