Minify JS

Status
Not open for further replies.

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
Jackass JoeJoe":39qyh9dx said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.

No, he's actually quite wrong: Minify vs gzip

First, it's the client that needs to tell the server that they even accept gzip encoded data. If the client doesn't, then there is no compression.

Second, compression and decompression takes time. The more data that needs to be decompressed, the longer decompression takes. Why allocate time to make sure whitespace gets recreated?

The real answer? You need to do both as it covers the most bases.

Clients that don't want gziped content still get some benefit because the minified file is smaller. Clients that do accept gzip get the benefits of both.

Also, since it is SOOO freaking easy to have minification done as part of a build process, or even on demand when the file is first requested, there's literally no excuse not to do this for your production deployments.
 
drogin":1qn8x00m said:
Jackass JoeJoe":1qn8x00m said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
... a bunch of garbage ...

Learn to ignore those that can't tell when something is or is not important to your case. What you see above is one of those - an i d Ten Tee litmus test.
 
I actually think all the responses so far have missed the real question from the OP.

Minifying (from what I can tell, I could be wrong) is taking an existing javascript script/file and making it as short as possible. It does so by replacing variable or function names with single characters where possible and by reducing operations to the shortest form possible. The actual difference between minifying and compressing is that minifying actually changes the source code itself and takes out characters where possible, while compressing simply compresses the code as given.

Thus, the combination of minifying and compression is actually more effective than just one or the other. The two operations aren't directly comparable because they do entirely different things.
 

hanser

Ars Legatus Legionis
43,847
Subscriptor++
gaijin":3hgaamdq said:
I actually think all the responses so far have missed the real question from the OP.

Minifying (from what I can tell, I could be wrong) is taking an existing javascript script/file and making it as short as possible. It does so by replacing variable or function names with single characters where possible and by reducing operations to the shortest form possible. The actual difference between minifying and compressing is that minifying actually changes the source code itself and takes out characters where possible, while compressing simply compresses the code as given.

Thus, the combination of minifying and compression is actually more effective than just one or the other. The two operations aren't directly comparable because they do entirely different things.
This is correct.

The underlying, more important question is whether it's worth the time and effort required to minify and then gzip. In an automated deployment system, minification is trivial. In the case of a personal blog or something, it's probably not worth the hassle.
 
hanser":3dgcu4c5 said:
The underlying, more important question is whether it's worth the time and effort required to minify and then gzip. In an automated deployment system, minification is trivial. In the case of a personal blog or something, it's probably not worth the hassle.
Yeah, now that I look, the OP actually has a number of questions. I just wanted to clear up the last one (what minifying does).

The question for the OP is 1. why he wants to optimize his website, and 2. why he thinks minifying would help. And probably 3. what is he doing already.
 

Pont

Ars Legatus Legionis
26,160
Subscriptor
Jackass JoeJoe":31xzoiwn said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
It's completely worth it on any high volume site. Rule of thumb, if you're colocating your servers or using a CDN, it's worth it to minify your JS.

It's not just about the $$$ you save in bandwidth, either. If your page doesn't finish rendering until your javascript is loaded (quite common, actually), it makes an even bigger difference in perceived performance. To someone on a slow or packet-lossy connection, like a smartphone with poor reception, it can make a world of difference.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Pont":38u5qcnl said:
Jackass JoeJoe":38u5qcnl said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
It's completely worth it on any high volume site. Rule of thumb, if you're colocating your servers or using a CDN, it's worth it to minify your JS.

It's not just about the $$$ you save in bandwidth, either. If your page doesn't finish rendering until your javascript is loaded (quite common, actually), it makes an even bigger difference in perceived performance. To someone on a slow or packet-lossy connection, like a smartphone with poor reception, it can make a world of difference.
Closure Compiler outputs more efficient JavaScript in a lot of cases, too. Jackass JoeJoe is living down to his name.
 

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
Jackass JoeJoe":7vn2b8cr said:
drogin":7vn2b8cr said:
Jackass JoeJoe":7vn2b8cr said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
... a bunch of garbage ...

Learn to ignore those that can't tell when something is or is not important to your case. What you see above is one of those - an i d Ten Tee litmus test.

What exactly did I say that was incorrect or garbage?

You're saying I should be ignored because I can't tell if minification applies to his case, but his case was vaguely stated as "optimizing websites".

Why responds just to be a douche? By all means point out what I said that was incorrect. It looks to me like pretty much everyone else has echoed what I already said...
 

LimpBagel

Ars Legatus Legionis
11,778
Another thing that will help in addition to minifying/compressing your JS is to reduce the number of requests. HTTP requests have a minimum overhead no matter how small the files are. If you always include jquery, jquery UI, some other library, and then your own JS functions, you can put those all into one file and save 3 HTTP requests. It's the same idea as using sprites if you have a bunch of images.

This all applies to CSS files, by the way.

EDIT:

You should also serve these static files (css/js/images) from either a CDN or a domain that doesn't have a cookie set. When you serve them from your main domain, you are including whatever cookies you have set with every single requests. That's just wasted bandwidth. If you don't have the budget or need for a CDN, you can achieve the same effect from hosting your files on a different subdomain like http://static.domain.com/js/jquery.min.js or something similar. The one pitfall there is you will still send cookies if your main cookie is set on domain.com and not www.domain.com. If you've already set them on the wildcard, you'll have to purchase a new domain and use that.

Another benefit here is you can crank the caching on these files way up and use version numbers in the name when you push an update. I've done that before and it can be a pain to manage but if you plan it out in advance it's not so bad and can make a noticeable difference on page load times.
 

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
LimpBagel":t1rnfgwj said:
Another thing that will help in addition to minifying/compressing your JS is to reduce the number of requests. HTTP requests have a minimum overhead no matter how small the files are. If you always include jquery, jquery UI, some other library, and then your own JS functions, you can put those all into one file and save 3 HTTP requests. It's the same idea as using sprites if you have a bunch of images.

This all applies to CSS files, by the way.

EDIT:

You should also serve these static files (css/js/images) from either a CDN or a domain that doesn't have a cookie set. When you serve them from your main domain, you are including whatever cookies you have set with every single requests. That's just wasted bandwidth. If you don't have the budget or need for a CDN, you can achieve the same effect from hosting your files on a different subdomain like http://static.domain.com/js/jquery.min.js or something similar. The one pitfall there is you will still send cookies if your main cookie is set on domain.com and not http://www.domain.com. If you've already set them on the wildcard, you'll have to purchase a new domain and use that.

Another benefit here is you can crank the caching on these files way up and use version numbers in the name when you push an update. I've done that before and it can be a pain to manage but if you plan it out in advance it's not so bad and can make a noticeable difference on page load times.

You actually need to test this based on your case to find the sweet spot. There's a point at which it is faster to load several scripts in parallel instead of one giant ass file.

Also, depending on the type of application you're building you may wind up loading more scripts than you need to on each page. In that case, it's a better client experience to download only the scripts you need on a page instead of ALL the scripts combined.

I like YepNope as one solution to this. It's very good for loading browser hacks and pollyfills as well as conditionally loading things like jQuery plugins or custom scripts.
 
drogin":w544vh6j said:
Jackass JoeJoe":w544vh6j said:
drogin":w544vh6j said:
Jackass JoeJoe":w544vh6j said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
... a bunch of garbage ...

Learn to ignore those that can't tell when something is or is not important to your case. What you see above is one of those - an i d Ten Tee litmus test.

What exactly did I say that was incorrect or garbage?

You're saying I should be ignored because I can't tell if minification applies to his case, but his case was vaguely stated as "optimizing websites".

Why responds just to be a douche? By all means point out what I said that was incorrect. It looks to me like pretty much everyone else has echoed what I already said...

I guess you're too busy programming in pure assembly to get it.

First rule of being logical is staying on topic. First, you didn't address the point being made. This is the favorite trick of a politician, just keep talking until the audience forgets the original point. I made two assertions, you didn't address them at all and went on a pure non-sequitur. Go read Hanser's post, since he clearly understands exactly the points I made, which are nothing but pure pragmatism. Go read http://en.wikipedia.org/wiki/You_ain't_gonna_need_it until you actually believe it.
 
Blacken00100":3q6xbq2t said:
Pont":3q6xbq2t said:
Jackass JoeJoe":3q6xbq2t said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
It's completely worth it on any high volume site. Rule of thumb, if you're colocating your servers or using a CDN, it's worth it to minify your JS.

It's not just about the $$$ you save in bandwidth, either. If your page doesn't finish rendering until your javascript is loaded (quite common, actually), it makes an even bigger difference in perceived performance. To someone on a slow or packet-lossy connection, like a smartphone with poor reception, it can make a world of difference.
Closure Compiler outputs more efficient JavaScript in a lot of cases, too. Jackass JoeJoe is living down to his name.

You think that Pont said anything contradictory to my statements? I completely agree with his examples of extreme cases. It's not making too much of a logical leap to say argue he's proving my second point in spades.
 

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
Jackass JoeJoe":3vqm7lbm said:
drogin":3vqm7lbm said:
Jackass JoeJoe":3vqm7lbm said:
drogin":3vqm7lbm said:
Jackass JoeJoe":3vqm7lbm said:
No, you're not missing anything. It would take an extreme case for this to be worth it. If you had one of these cases, you'd likely already know all about it.
... a bunch of garbage ...

Learn to ignore those that can't tell when something is or is not important to your case. What you see above is one of those - an i d Ten Tee litmus test.

What exactly did I say that was incorrect or garbage?

You're saying I should be ignored because I can't tell if minification applies to his case, but his case was vaguely stated as "optimizing websites".

Why responds just to be a douche? By all means point out what I said that was incorrect. It looks to me like pretty much everyone else has echoed what I already said...

I guess you're too busy programming in pure assembly to get it.

First rule of being logical is staying on topic. First, you didn't address the point being made. This is the favorite trick of a politician, just keep talking until the audience forgets the original point. I made two assertions, you didn't address them at all and went on a pure non-sequitur. Go read Hanser's post, since he clearly understands exactly the points I made, which are nothing but pure pragmatism. Go read http://en.wikipedia.org/wiki/You_ain't_gonna_need_it until you actually believe it.

You actually made three assertions:

1. That the OP wasn't missing anything regarding the difference between minifying and gziping scripts.
2. That only "extreme" cases warranted compressing scripts (you didn't quantify extreme).
3. That the OP would already know how to properly compress scripts if his case qualified as extreme.

Assertion one is provably false, which was the content of my response. It can be demonstrated that minifying and compressing is optimal in a variety of cases.

Assertion two is worthless for two reasons. First, the OP didn't provide enough information as to whether his case was "extreme" or not. Your assumption is that it isn't, which is frankly irrelevant. Second, you didn't quantify what you considered "extreme" which is problematic in that its a subjective metric.

Assertion three is completely circular and is the equivalent to saying nothing in the first place.
 

Pont

Ars Legatus Legionis
26,160
Subscriptor
IMOP, there are plenty of cases where minifying is justified that are far from "extreme" cases.

If your JS-heavy site gets an extra 0.2 seconds in perceived "page is loaded and ready" time, it's worth it. Users may not realize that they notice it, but the research shows that they do. All else being equal, they will pick the more responsive site.
 

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
Pont":1czzgs4f said:
IMOP, there are plenty of cases where minifying is justified that are far from "extreme" cases.

If your JS-heavy site gets an extra 0.2 seconds in perceived "page is loaded and ready" time, it's worth it. Users may not realize that they notice it, but the research shows that they do. All else being equal, they will pick the more responsive site.

Yes, I believe the research from Amazon says that every 100ms increase in wait time reduces sales by 1%.
 
Status
Not open for further replies.