Ask HN: Do you recommend compressing js files to remove extra whitespace?
I feel like I am using a lot of javascript on my new project and I am wondering if it's a good idea to run my JS files through a service like 'jscompress.com' to speed up execution time.
These compression services mostly strip out "extra whitespace" and "comments" to speed up execution times, but compressing makes the file look messy.
These online compression services allow you to do a couple of things, but I am not sure which is more beneficial (if at all).
Option #1: Upload and combine js files to consolidate and compress.
Option #2: Just compress files keeping them separate.
If anyone has a recommendation please share. Thanks fellow hackers.
(
10 comments
| 7 points
| on HN
)
As for the 2 options you listed, there are 2 benefits of option 1 over 2 (given that no one is ever going to look at the compressed files). First, some JS compressors do a better job when they have all the files together as one large file. Second, consolidating them all down to 1 file significantly decreases the number of requests the client has to make to your server. This reason alone will probably have a bigger impact on your sites performance than compressing the files alone so I would definitely recommend you look into it.
I tend to use YUIcompressor, but I don't think any of them differ much.
Consolidating the files reduces the number of HTTP requests the browser has to make, which can make the page load faster because each request has some overhead. On the other hand, JavaScript libraries can be loaded from a CDN like http://code.google.com/apis/libraries/ , and if you do that users may already have a copy in their cache, whereas if you load one combined script including a library and your own code from your own site, users won't get that caching benefit.
Finally, debugging minified JavaScript is a serious pain, so don't minify any JavaScript in active development, just when you are ready to move to production.
It's generally good practice to consolidate and minify your js files, but don't waste time planning how you do that during your development phases.
The reason I ask if you are having performance problems or page loading performance problems is that generally people get hung up this, and often the code files are the least of your worries. Badly created images and many of them can be a killer - try to look at what you can do here first.
That way, you can work with a file formatted to your liking, but when deployed, it will be optimized.
Also, I recommend the Google Closure compiler instead of jscompress. Just leave it set at simple unless you are explicitly using Closure. http://closure-compiler.appspot.com/home