From: Mihai Bazon Date: Mon, 5 Nov 2012 11:13:06 +0000 (+0200) Subject: minor optimization X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=774f2ded9422903f082f0291ec69c7a6e2edfcaf;p=UglifyJS.git minor optimization for `==` or `!=` against a constant, prefer to display the constant first. should help a bit after gzip, i.e.: typeof foo=="undefined" ^^^^^^ ^^^^^^^^^^^^^ vs: "undefined"==typeof foo ^^^^^^^^^^^^^^^^^^^ (longer sequence that could repeat) idea stolen from closure. --- diff --git a/lib/compress.js b/lib/compress.js index fe212b42..5cbb31c9 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1489,21 +1489,15 @@ merge(Compressor.prototype, { // XXX: intentionally falling down to the next case case "==": case "!=": - if (self.left instanceof AST_UnaryPrefix - && self.left.operator == "typeof" - && self.right instanceof AST_String - && self.right.value == "undefined") { - if (!(self.left.expression instanceof AST_SymbolRef) - || !self.left.expression.undeclared()) { - self.left = self.left.expression; - self.right = make_node(AST_Undefined, self.right).optimize(compressor); - if (self.operator.length == 2) self.operator += "="; - } + if (self.right instanceof AST_Constant && !(self.left instanceof AST_Constant)) { + var tmp = self.left; + self.left = self.right; + self.right = tmp; } - else if (self.left instanceof AST_String - && self.left.value == "undefined" - && self.right instanceof AST_UnaryPrefix - && self.right.operator == "typeof") { + if (self.left instanceof AST_String + && self.left.value == "undefined" + && self.right instanceof AST_UnaryPrefix + && self.right.operator == "typeof") { if (!(self.right.expression instanceof AST_SymbolRef) || !self.right.expression.undeclared()) { self.left = self.right.expression;