Javascript minification of comparison statements

前端 未结 2 1262
天涯浪人
天涯浪人 2020-12-20 22:27

I was looking over one of the minified js files generated by closure. I found that wherever I\'m checking for equality between a variable and string like,

a          


        
2条回答
  •  青春惊慌失措
    2020-12-20 23:01

    This is done as for a minor gzip compression advantage. If you have "x == 1" and "1 == x" the compiler switches it to "1 == x" in both cases and you get more regular code that compresses better. The win is so minor, that I've considered deleting the code and saving the cpu cycles, but it is on for now. It has nothing to do with preventing programmer errors as it will never switch "x = 2" into "2 = x" as that would change the meaning of the program.

提交回复
热议问题