简体   繁体   中英

Can I assume gzip compression for my js files?

In the past, I have been writing code like this to reduce the size of my JavaScript files (just exaggerated here for the purpose of the question):

var u="http://mySite.com/TeamNumber",
    e=".jpg",
    t="team",
    image=i={};
i[t+1]=u+"One/team1"+e;
i[t+2]=u+"Two/team2"+e; 
i[t+3]=u+"Three/team3"+e;

Now, if I understand correctly, this is pretty much useless if the http transfer uses gzip compression, as duplicate text will be compressed anyway.

Am I correct to assume that all servers/browsers now rely on gzip compression, and that the below script, which on paper is 10% bigger, will actually have the same transfer size as the above one:

var urlRoot="http://mySite.com/";
var images={
    team1: urlRoot + "TeamNumberOne/team1.jpg",
    team2: urlRoot + "TeamNumberTwo/team2.jpg",
    team3: urlRoot + "TeamNumberThree/team3.jpg"
};

Note that my question is related to file compression, not minification.

Experimentation is usually the easier way to tell: your first snippet of code is 132 bytes when gzip-compressed, and the second one is 138 bytes. Hardly a noticeable difference.

In the end, it's really not worth it to write your code like this, as it makes it hardly readable.

You're really doing two things here, and you should leave those up the appropriate tooling:

  • Reducing duplication. Leave that up to compression.
  • Using short identifiers. Leave that up to a minifier.

Note that reducing duplication is usually a good thing, but of course isn't when you use team+1 to write "team1" which, among other things, makes the identifier "un-grep-able".

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