You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionexample(value,returnSelf){returnvoid0===value// if (value === undefined)
? void0// { return undefined }
: someConditional()// if (someConditional())
? value// { return value }
: doSomething(value);// return doSomething() }
If value === undefined, we could instead return value directly (instead of the literal undefined). This preserves the same behavior but allows the optimizer to produce more compact code:
if(value===undefined){returnvalue;}
With this change, the optimizer could simplify the ternary logic to:
functionexample(value,returnSelf){returnvoid0===value||someConditional()// if (value === undefined || someConditional())
? value// { return value }
: doSomething(value);// return doSomething()
This approach should work for any strict equality comparison (e.g., === true, numbers, strings, etc.), as long as the returned value matches the compared value.
Yeah I also think that's the cause. I'll finish #10202 asap and publish a new version.
My plan was to avoid publishing until #10202 is done, but I needed to publish a new version for nodejs folks.
Describe the feature
Hello! I wanted to suggest an optimization for how SWC handles strict equality checks where the returned
value
matches the compared value.Take the following example:
Currently, this is optimized into:
If
value === undefined
, we could instead returnvalue
directly (instead of the literalundefined
). This preserves the same behavior but allows the optimizer to produce more compact code:With this change, the optimizer could simplify the ternary logic to:
This approach should work for any strict equality comparison (e.g.,
=== true
, numbers, strings, etc.), as long as the returned value matches the compared value.Thanks for considering this optimization! 😊
Babel plugin or link to the feature description
No response
Additional context
The text was updated successfully, but these errors were encountered: