gzip compression will reduce identical strings to a byte or two. Use that and it's a non-issue. :)
Answer from Dagg Nabbit on Stack Overflowgzip compression will reduce identical strings to a byte or two. Use that and it's a non-issue. :)
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.
[AskJS] Are there JS minifiers that can compress the code by storing and reusing repeating property/method names and strings?
How to minify HTML, CSS, and Javascript at the same time?
What is JavaScript Minification?
How does JavaScript Minification work?
Why is Minification used?
Are there JS minifiers that can transform a code like this
const obj = new A;
obj.longMethodName();
to something like this in the case of multiple usages of methods/properties/classes with long names
var a = 'longMethodName', o = new A;
o[a]();
The same with repeating long strings.
» npm install string-minify
» npm install html-minifier