Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean conditions not fully optimized #979

Closed
ThomasR opened this issue Feb 20, 2016 · 3 comments
Closed

Boolean conditions not fully optimized #979

ThomasR opened this issue Feb 20, 2016 · 3 comments

Comments

@ThomasR
Copy link

ThomasR commented Feb 20, 2016

Consider this simple code

if (a == 1 || b == 2) {
  foo();
}

It gets compressed to

(1==a||2==b)&&foo();

This is not optimal. 1!=a&&2!=b||foo(); is shorter.


Strange enough, I found that when compressing the equivalent code

if (!(a == 1 || b==2 )) {} else {
  foo();
}

the result is indeed

1!=a&&2!=b||foo();

as requested.

@kzc
Copy link
Contributor

kzc commented Feb 21, 2016

I have a fix for this. Will create a PR tomorrow.

if (a == 1 || b == 2) foo();

Uglify is aware of the negative condition optimization. This issue is occurring because the length of the original condition 1==a||2==b and the negated condition 1!=a&&2!=b are the same so uglify bases its optimization on the original. But the code doesn't take into account that || has lower precedence than && in the expression (1==a||2==b)&&foo();, and as such the solution with the original condition is 2 (paren) characters longer than the solution with the negated condition 1!=a&&2!=b||foo();

@kzc
Copy link
Contributor

kzc commented Feb 21, 2016

Fix in PR #981

@ThomasR
Copy link
Author

ThomasR commented Feb 21, 2016

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants