简体   繁体   中英

What is the best way to pack JavaScript code without getting performance flaws?

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?

使用JSMin并避免使用真正更耗CPU且速度更慢的打包 “放气”

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM