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

Determining that == instead of === is safe for number operators #412

Closed
Qantas94Heavy opened this issue Jan 29, 2014 · 5 comments
Closed

Comments

@Qantas94Heavy
Copy link

Let's say that we have the following code:

num % 1 === 0

Now assuming UglifyJS doesn't eliminate this code as side-effect free, it will return the following code:

num%1===0

Now the % operator is defined to return a number (even if it's NaN), so no type conversion is taking place. If we were minifying this manually, we could change === to ==:

num%1==0

which would be no different. Note UglifyJS is able to do this with stuff like booleans:

alert(!troll === false);

is converted to:

alert(!troll==!1);

Can this be done for number operators too?

@curiousdannii
Copy link

Note that ==! is still the strict inequality operator. If it was doing what you were suggesting it would be =!.

@Qantas94Heavy
Copy link
Author

!== is the strict inequality operator, not ==!. 😉 This is actually the abstract equality operator applied to !troll and !1 (shorthand for false).

@rvanvelzen
Copy link
Collaborator

It would probably be best to first implement some form of type inferencing. Otherwise it will only get messier.

@curiousdannii
Copy link

Oops yes, how did I get that wrong? :/

@alexlamsl
Copy link
Collaborator

Implementing is_number() in the same style as is_boolean() and is_string() would help to enable this optimisation.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Feb 28, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Feb 28, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Feb 28, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Feb 28, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 1, 2017
- `unsafe_math` for unsafe associative operations

fixes mishoo#412
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 3, 2017
- `unsafe_math` for unsafe associative operations

fixes mishoo#412
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 3, 2017
safe operations
- `m === n` => `m == n`
- `a + -b`  => `a - b`
- `-a + b`  => `b - a`
- `a+ +b`   => `+b+a`

`unsafe_math` associative operations
- `a + (b + c)`       => `(a + b) + c`
- `(n + 2) + 3`       => `5 + n`
- `(2 * n) * 3`       => `6 * n`
- `(a | 1) | (2 | d)` => `(3 | a) | b`

fixes mishoo#412
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 3, 2017
safe operations
- `a === b` => `a == b`
- `a + -b`  => `a - b`
- `-a + b`  => `b - a`
- `a+ +b`   => `+b+a`

`unsafe_math` associative operations
- `a + (b + c)`       => `(a + b) + c`
- `(n + 2) + 3`       => `5 + n`
- `(2 * n) * 3`       => `6 * n`
- `(a | 1) | (2 | d)` => `(3 | a) | b`

fixes mishoo#412
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 3, 2017
safe operations
- `a === b` => `a == b`
- `a + -b`  => `a - b`
- `-a + b`  => `b - a`
- `a+ +b`   => `+b+a`

associative operations
(bit-wise operations are safe, otherwise `unsafe_math`)
- `a + (b + c)`       => `(a + b) + c`
- `(n + 2) + 3`       => `5 + n`
- `(2 * n) * 3`       => `6 * n`
- `(a | 1) | (2 | d)` => `(3 | a) | b`

fixes mishoo#412
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 3, 2017
safe operations
- `a === b` => `a == b`
- `a + -b`  => `a - b`
- `-a + b`  => `b - a`
- `a+ +b`   => `+b+a`

associative operations
(bit-wise operations are safe, otherwise `unsafe_math`)
- `a + (b + c)`       => `(a + b) + c`
- `(n + 2) + 3`       => `5 + n`
- `(2 * n) * 3`       => `6 * n`
- `(a | 1) | (2 | d)` => `(3 | a) | b`

fixes mishoo#412
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

4 participants