Skip to content

feat(minifier): minify x ? 1 : 0 to +x or +!!x#20594

Merged
Boshen merged 4 commits intooxc-project:mainfrom
costajohnt:feat/minify-ternary-numeric
Mar 23, 2026
Merged

feat(minifier): minify x ? 1 : 0 to +x or +!!x#20594
Boshen merged 4 commits intooxc-project:mainfrom
costajohnt:feat/minify-ternary-numeric

Conversation

@costajohnt
Copy link
Copy Markdown
Contributor

Closes #20288

Adds numeric conditional expression optimization to the minifier:

  • x ? 1 : 0+x when x is known boolean (saves 3 chars)
  • x ? 1 : 0+!!x when x is unknown type and no parens needed (saves 1 char)
  • x ? 0 : 1+!x when no parens needed (saves 2 chars)
  • Skips when parentheses would make the result the same length or longer (e.g., a+b?1:0)

Profitability checks are based on the lessons from swc-project/swc#9920.

Implementation note: uses DetermineValueType to check if the test expression is boolean, and a simple test_needs_parens helper (matching BinaryExpression, LogicalExpression, etc.) to determine if wrapping would negate the savings.

@github-actions github-actions bot added A-minifier Area - Minifier C-enhancement Category - New feature or request labels Mar 21, 2026
@costajohnt costajohnt marked this pull request as ready for review March 21, 2026 04:17
@Boshen Boshen self-assigned this Mar 21, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 21, 2026

Merging this PR will not alter performance

✅ 49 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing costajohnt:feat/minify-ternary-numeric (6007da7) with main (2244984)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@camc314
Copy link
Copy Markdown
Contributor

camc314 commented Mar 21, 2026

@costajohnt can you run just minsize, cargo ck and just allocs to udpate the snapshots and fix CI?

costajohnt and others added 3 commits March 21, 2026 12:54
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.
@costajohnt costajohnt force-pushed the feat/minify-ternary-numeric branch from cd6aacf to b8f9865 Compare March 21, 2026 19:57
Replace `(Some(1.0), Some(val)) if val == 0.0` with direct
`(Some(1.0), Some(0.0))` pattern matching per Clippy lint.
@Boshen Boshen merged commit 2917bb2 into oxc-project:main Mar 23, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minifier: Minify x ? 1 : 0 to +x

3 participants