-
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
request: minify Boolean(foo) to (!!foo) #2962
Comments
Related: Same appears to occur with |
Non-new
While these two will usually return the same result, the non-new
While these type of optimizations can be useful, they enter into one of the more complex areas of the ECMAScript spec where the transforms can result in subtle but potentially critical differences in runtime behavior. |
Thanks for taking a look! The notes on String coercion do appear on the MDN (I didn't research String as heavily). Similarly, |
Hi!
I would like to able to write
Boolean(foo)
in my code which is clearer, but have esbuild minify it down to!!foo
which is faster. That's becauseBoolean(value)
is actually an alias for(new Boolean(foo)).valueOf()
.I've researched it a bit and seems it's an safe conversion. You'd probably have to wrap it in parentheses to ensure things like
Boolean(value).constructor
yields(!!value).constructor
, though I wasn't be surprised if there's another optimization step that handles that. Also,new Boolean(value)
would need to be guarded against.Some points of research:
The text was updated successfully, but these errors were encountered: