簡體   English   中英

為什么Google Closure交換參數?

[英]Why does Google Closure swap arguments?

我已經看到Google Closure編譯器在if子句中做了很多重寫。 例如:

if (a === 3) {…}

轉向

if (3 === a) {…}

如果原語是第一個參數,那么在JavaScript中比較速度更快,或者這是什么原因?

來自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.
 *
 */

正如Google封閉編譯器貢獻者所述,代碼注釋所指的壓縮意味着gzip壓縮,而不是實際的縮小“壓縮”。 它可以改進gzip壓縮的原因是,如果你的代碼中有0 === xx === 0 ,那么閉包編譯器會將這兩個規范化為0 === x ,這是重復的文本,因此可以更好地壓縮。

然后還有:

typeof this.value == "object"

typeof this.key == "object"

唯一的字符串是: typeof this. valuekey== "object"

但如果你重新訂購:

"object" == typeof this.value

"object" == typeof this.key

唯一的字符串是: "object" == typeof this. valuekey 較少的獨特字符串和相當長的重復字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM