feat(minifier): minify x ? 1 : 0 to +x or +!!x#20594
Merged
Boshen merged 4 commits intooxc-project:mainfrom Mar 23, 2026
Merged
feat(minifier): minify x ? 1 : 0 to +x or +!!x#20594Boshen merged 4 commits intooxc-project:mainfrom
x ? 1 : 0 to +x or +!!x#20594Boshen merged 4 commits intooxc-project:mainfrom
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Contributor
|
@costajohnt can you run |
Add numeric conditional expression optimization: - `x ? 1 : 0` → `+x` when x is known boolean - `x ? 1 : 0` → `+!!x` when x is unknown type (no parens needed) - `x ? 0 : 1` → `+!x` (no parens needed) - Skip when parentheses would make the result longer Based on the approach discussed in oxc-project#20288, with profitability checks inspired by swc-project/swc#9920. Closes oxc-project#20288
Remove unused `GetPrecedence` import and regenerate minsize/allocs snapshots after rebase.
cd6aacf to
b8f9865
Compare
Replace `(Some(1.0), Some(val)) if val == 0.0` with direct `(Some(1.0), Some(0.0))` pattern matching per Clippy lint.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #20288
Adds numeric conditional expression optimization to the minifier:
x ? 1 : 0→+xwhenxis known boolean (saves 3 chars)x ? 1 : 0→+!!xwhenxis unknown type and no parens needed (saves 1 char)x ? 0 : 1→+!xwhen no parens needed (saves 2 chars)a+b?1:0)Profitability checks are based on the lessons from swc-project/swc#9920.
Implementation note: uses
DetermineValueTypeto check if the test expression is boolean, and a simpletest_needs_parenshelper (matchingBinaryExpression,LogicalExpression, etc.) to determine if wrapping would negate the savings.