Why does Google Closure replace arguments?

I saw how the Google Closure compiler rewrites a lot in if-clauses. For example:

if (a === 3) {…} 

turns into

 if (3 === a) {…} 

Are comparisons faster in JavaScript if the primitive is the first argument, or what causes this?

+8
javascript google-closure
source share
1 answer

From ReorderConstantExpression.java :

 /** * Reorder constant expression hoping for a better compression. * ex. x === 0 -> 0 === x * After reordering, expressions like 0 === x and 0 === y may have higher * compression together than their original counterparts. * */ 

As pointed out by the google closure compiler , code comment compression refers to gzip compression, and not to the actual reduction of "compression". The reason it can improve gzip compression is because if your code has 0 === x and x === 0 , the closure compiler normalizes both of them to 0 === x , which duplicates the text and, thus, shrinks better.

Then there is also:

 typeof this.value == "object" typeof this.key == "object" 

Unique Strings: typeof this. , value , key and == "object"

But if you change the order:

 "object" == typeof this.value "object" == typeof this.key 

Unique lines: "object" == typeof this. , value and key . Less unique lines and a rather long duplicate.

+16
source share

All Articles