Optimize conditionals where the condition and consequent are equivalent and have no side effects
This commit is contained in:
parent
f0c1a01bc2
commit
c662ff0eb5
|
|
@ -2332,6 +2332,15 @@ merge(Compressor.prototype, {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// x=y?y:z --> x=y||z
|
||||||
|
if (self.condition.equivalent_to(consequent)
|
||||||
|
&& !self.condition.has_side_effects(compressor)) {
|
||||||
|
return make_node(AST_Binary, self.condition, {
|
||||||
|
left: self.condition,
|
||||||
|
operator: "||",
|
||||||
|
right: alternative
|
||||||
|
});
|
||||||
|
}
|
||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -293,3 +293,38 @@ cond_7: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cond_8: {
|
||||||
|
options = {
|
||||||
|
conditionals: true,
|
||||||
|
evaluate : true
|
||||||
|
};
|
||||||
|
input: {
|
||||||
|
// compress these
|
||||||
|
a = condition ? condition : b;
|
||||||
|
|
||||||
|
if (condition) {
|
||||||
|
a = condition;
|
||||||
|
} else {
|
||||||
|
a = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
a = condition ? condition : b();
|
||||||
|
|
||||||
|
// Don't compress conditions that have side effects
|
||||||
|
if (condition()) {
|
||||||
|
a = condition();
|
||||||
|
} else {
|
||||||
|
a = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
a = condition() ? condition() : b;
|
||||||
|
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
a = condition || b;
|
||||||
|
a = condition || b;
|
||||||
|
a = condition || b();
|
||||||
|
a = condition() ? condition() : b;
|
||||||
|
a = condition() ? condition() : b;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user