minor optimization
authorMihai Bazon <mihai@bazon.net>
Wed, 2 Oct 2013 12:31:31 +0000 (15:31 +0300)
committerMihai Bazon <mihai@bazon.net>
Wed, 2 Oct 2013 12:31:31 +0000 (15:31 +0300)
unlikely to help in hand-written code:

    (something() ? foo : bar) == foo  ==>  something()

lib/compress.js

index daac1c3..93f0148 100644 (file)
@@ -1882,6 +1882,32 @@ merge(Compressor.prototype, {
                 // result.  hence, force switch.
                 reverse(null, true);
             }
+            if (/^[!=]==?$/.test(self.operator)) {
+                if (self.left instanceof AST_SymbolRef && self.right instanceof AST_Conditional) {
+                    if (self.right.consequent instanceof AST_SymbolRef
+                        && self.right.consequent.definition() === self.left.definition()) {
+                        if (/^==/.test(self.operator)) return self.right.condition;
+                        if (/^!=/.test(self.operator)) return self.right.condition.negate(compressor);
+                    }
+                    if (self.right.alternative instanceof AST_SymbolRef
+                        && self.right.alternative.definition() === self.left.definition()) {
+                        if (/^==/.test(self.operator)) return self.right.condition.negate(compressor);
+                        if (/^!=/.test(self.operator)) return self.right.condition;
+                    }
+                }
+                if (self.right instanceof AST_SymbolRef && self.left instanceof AST_Conditional) {
+                    if (self.left.consequent instanceof AST_SymbolRef
+                        && self.left.consequent.definition() === self.right.definition()) {
+                        if (/^==/.test(self.operator)) return self.left.condition;
+                        if (/^!=/.test(self.operator)) return self.left.condition.negate(compressor);
+                    }
+                    if (self.left.alternative instanceof AST_SymbolRef
+                        && self.left.alternative.definition() === self.right.definition()) {
+                        if (/^==/.test(self.operator)) return self.left.condition.negate(compressor);
+                        if (/^!=/.test(self.operator)) return self.left.condition;
+                    }
+                }
+            }
         }
         self = self.lift_sequences(compressor);
         if (compressor.option("comparisons")) switch (self.operator) {