views:

60

answers:

3

I was looking over some of our JavaScript compression and noticed that no strings that aren't object property names are minified into variables.

For example, let's say I have these two pieces of code in my script:

alert("Wrong answer");
alert("Wrong answer buddy");

The minification we get from YUI and Closure, if I'm not mistaken, is:

alert("Wrong answer");alert("Wrong answer buddy");

I would think the resulting minification would look like this:

var a="Wrong answer";alert(a);alert(a+" buddy");

Do any minification tools do this? What's stopping our tools from doing this?

A: 

The compression tools have their limits and you found one. You should create your own variables for the strings, then the tools will compress the variable names.

Eg
var msg_wrong = "Wrong answer",
    msg_very_wrong = msg_wrong + "!!!";
alert (msg_wrong);
alert (msg_very_wrong);


// "msg_wrong" and "msg_very_wrong" variable names will be compressed
Larry K
+1  A: 

If I were to hazard a guess, I would say that it is because the compiler cannot tell when it would be more optimal to break out your strings into variables and concatenate them. (And, as such things go, it falls under the heading of micro-optimization in the first place.)

For example, the following code:

if (test_case==="He fell down a well") {
    var i=my_object_array.length-1;
    while(i>=0) {
        my_object_array[i].say("He fell down a well did he Lassie?");
        i--;
    }
}

Might be re-rendered by your theoretical compiler as:

var x="He fell down a well";
if (a===x) {
    var i=b.length-1;
    while(i>=0) {
        b[i].say(x+" did he Lassie?");
        i--;
    }
}

... which would of course, increase the time it takes for the while loop to complete its work.

Of course, a slightly more intelligent compiler might recognize that trap and optimize still further:

var x="He fell down a well";
var x1=x+" did he Lassie?";
if (a===x) {
    var i=b.length-1;
    while(i>=0) {
        b[i].say(x1);
        i--;
    }
}

Either way though, a good javascript compiler, to my mind, optimizes the code first for performance, and only secondarily for character count. As this is an optimization primarily for character count improvement I think that there probably simply has not been enough demand for it to warrant the time of the people who maintain Closure and the YUI Compressor.

Sean Vieira
+4  A: 

gzip compression will reduce identical strings to a byte or two. Use that and it's a non-issue. :)

no
That doesn't sound accurate to me. Why doesn't gzip reduce all identical strings (e.g. variabale names) to one or two bytes also then? To gzip, "foobar" as a variable name is no different than "foobar" as a string literal.
CantSleepAgain
It _does_ do that; that's what I meant by "strings." Any time a sequence of bytes occurs that has already occurred it should be reduced to just a few bytes.
no
I'm still not following you. Thank your for your patience :). Let's say my JavaScript has this code in it 50 times: "Company.Search.Hash.update()". If I were to create a variable out of this (e.g.: "var hash=Company.Search.Hash;") and reference that variable everywhere (e.g. "hash.update()") instead of using "Company.Search.Hash.update()", the minified version of the JavaScript will be smaller...
CantSleepAgain
...But you're saying it doesn't even matter? There's no point in making this change to the JavaScript for minification optimization because "Company.Search.Hash.update()" will be replaced with a few bytes everywhere anyway? Why do minifiers bother minifying variable names at all then if gzip effectively does this anyway?
CantSleepAgain
`var hash=Company.Search.Hash` and then repeating `hash` instead of repeating `Company.Search.Hash` is a good idea because it keeps your code easier to read (and it might be a _tiny_ speed optimization). Other than that, once gzip gets ahold of it, it doesn't matter. I assume minifiers only bother minifying variable names because gzip compression isn't always available (some shared hosts don't offer it).
no