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
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;