Skip to content

Optimization for conditionals returning the compared value directly #10250

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

Open
rentalhost opened this issue Mar 22, 2025 · 2 comments
Open

Optimization for conditionals returning the compared value directly #10250

rentalhost opened this issue Mar 22, 2025 · 2 comments
Assignees
Milestone

Comments

@rentalhost
Copy link

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:

function example(value) {  
  if (value === undefined) {  
    return undefined;  
  }  

  if (someConditional()) {  
    return value;  
  }  

  return doSomething(value);  
}  

Currently, this is optimized into:

function example(value, returnSelf) {  
    return void 0 === value     // if (value === undefined)  
      ? void 0                  // { 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) {  
  return value;  
}  

With this change, the optimizer could simplify the ternary logic to:

function example(value, returnSelf) {
    return void 0 === 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.

Thanks for considering this optimization! 😊

Babel plugin or link to the feature description

No response

Additional context

@Austaras Austaras self-assigned this Mar 23, 2025
@Austaras
Copy link
Member

In mainline of swc submitted code would be transformed into

export function example(value) {
    if (void 0 !== value) return someConditional() ? value : doSomething(value);
}

which doesn't contain return void 0. It's most possibly be caused by #10201

@kdy1
Copy link
Member

kdy1 commented Mar 23, 2025

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.

@kdy1 kdy1 added this to the Planned milestone May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants