Minimizing string literal strings?

I looked through some of our JavaScript scripts and noticed that not a single line that is not a property name of an object has been converted to variables.

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

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

The rigification we receive from YUI and Closure, if I am not mistaken, is as follows:

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

I would think that the resulting thumbnail would look like this:

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

Do you use any tools to minimize? What stops our tools from doing this?

+3
source share
3 answers

gzip compression will reduce identical lines to a byte or two. Use this and this is not a problem. :)

+7
source

, , , , . (, , - .)

, :

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--;
    }
}

, :

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--;
    }
}

... , , , while .

, :

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--;
    }
}

, javascript, , , . , , , , , , Closure YUI Compressor.

+2

Compression tools have their limitations, and you have found them. You must create your own variables for the strings, then the tools 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
+1
source

Source: https://habr.com/ru/post/1755652/


All Articles