-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
Note that ==! is still the strict inequality operator. If it was doing what you were suggesting it would be =!. |
|
It would probably be best to first implement some form of type inferencing. Otherwise it will only get messier. |
Oops yes, how did I get that wrong? :/ |
Implementing |
- `unsafe_math` for unsafe associative operations fixes mishoo#412
- `unsafe_math` for unsafe associative operations fixes mishoo#412
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
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
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
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
Let's say that we have the following code:
Now assuming UglifyJS doesn't eliminate this code as side-effect free, it will return the following code:
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==
:which would be no different. Note UglifyJS is able to do this with stuff like booleans:
is converted to:
Can this be done for number operators too?
The text was updated successfully, but these errors were encountered: