From 774f2ded9422903f082f0291ec69c7a6e2edfcaf Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Mon, 5 Nov 2012 13:13:06 +0200 Subject: [PATCH] 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. --- lib/compress.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) 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; -- 2.34.1