vitebonus / closure-library

Automatically exported from code.google.com/p/closure-library
0 stars 0 forks source link

Using local variable scope can make script longer #511

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago

What steps will reproduce the problem?
1. Run this code through Google Closure Compiler:
-----
function myFunction ()
{
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var i = 0;
    var enc1 = keyStr.indexOf(input.charAt(i++));
    var enc2 = keyStr.indexOf(input.charAt(i++));
    var enc3 = keyStr.indexOf(input.charAt(i++));
    var enc4 = keyStr.indexOf(input.charAt(i++));
    var enc5 = keyStr.indexOf(input.charAt(i++));
    var enc6 = keyStr.indexOf(input.charAt(i++));
    var enc7 = keyStr.indexOf(input.charAt(i++));
    var enc8 = keyStr.indexOf(input.charAt(i++));
    var enc9 = keyStr.indexOf(input.charAt(i++));
    var enc10 = keyStr.indexOf(input.charAt(i++));
}
-----

What is the expected output? What do you see instead?

I would expect JavaScript that is smaller.  Instead you get JavaScript script 
that is larger.

You end up with the contents of the keyStr variable being replicated, and is 
"compressed" down to this:

-----

function myFunction(){var 
a=0;"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(
input.charAt(a++));"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
89+/=".indexOf(input.charAt(a++));"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
tuvwxyz0123456789+/=".indexOf(input.charAt(a++));"ABCDEFGHIJKLMNOPQRSTUVWXYZabcd
efghijklmnopqrstuvwxyz0123456789+/=".indexOf(input.charAt(a++));"ABCDEFGHIJKLMNO
PQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(input.charAt(a++));
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(inpu
t.charAt(a++));"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
=".indexOf(input.charAt(a++));"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw
xyz0123456789+/=".indexOf(input.charAt(a++));"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh
ijklmnopqrstuvwxyz0123456789+/=".indexOf(input.charAt(a++));"ABCDEFGHIJKLMNOPQRS
TUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(input.charAt(a++))};

-----

But if you make the keyStr a global variable instead of a local variable, you 
get more of what I would expect:

-----

function 
myFunction(){keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
789+/=";var 
a=0;keyStr.indexOf(input.charAt(a++));keyStr.indexOf(input.charAt(a++));keyStr.i
ndexOf(input.charAt(a++));keyStr.indexOf(input.charAt(a++));keyStr.indexOf(input
.charAt(a++));keyStr.indexOf(input.charAt(a++));keyStr.indexOf(input.charAt(a++)
);keyStr.indexOf(input.charAt(a++));keyStr.indexOf(input.charAt(a++));keyStr.ind
exOf(input.charAt(a++))};

-----

What version of the product are you using? On what operating system?

The latest version (from July 11, 2012).  Using Java Runtime Environment (build 
1.7.0_05-b05) on openSUSE 11.3

Original issue reported on code.google.com by digitalp...@gmail.com on 24 Oct 2012 at 6:53

GoogleCodeExporter commented 8 years ago
First, please file closure-compiler bugs against the closure-compiler project
http://code.google.com/p/closure-compiler/issues/list

Second, this is intentional, and this FAQ item explains why the current 
behavior results in a smaller file:
http://code.google.com/p/closure-compiler/wiki/FAQ#Closure_Compiler_inlined_all_
my_strings,_which_made_my_code_size

Original comment by Nicholas.J.Santos on 24 Oct 2012 at 8:49