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.
This commit is contained in:
Mihai Bazon
2012-11-05 13:13:06 +02:00
parent 85af942d64
commit 774f2ded94

View File

@@ -1489,21 +1489,15 @@ merge(Compressor.prototype, {
// XXX: intentionally falling down to the next case // XXX: intentionally falling down to the next case
case "==": case "==":
case "!=": case "!=":
if (self.left instanceof AST_UnaryPrefix if (self.right instanceof AST_Constant && !(self.left instanceof AST_Constant)) {
&& self.left.operator == "typeof" var tmp = self.left;
&& self.right instanceof AST_String self.left = self.right;
&& self.right.value == "undefined") { self.right = tmp;
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 += "=";
}
} }
else if (self.left instanceof AST_String if (self.left instanceof AST_String
&& self.left.value == "undefined" && self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix && self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") { && self.right.operator == "typeof") {
if (!(self.right.expression instanceof AST_SymbolRef) if (!(self.right.expression instanceof AST_SymbolRef)
|| !self.right.expression.undeclared()) { || !self.right.expression.undeclared()) {
self.left = self.right.expression; self.left = self.right.expression;