Skip to content

feat(minifier): minimize switch statements#17523

Closed
armano2 wants to merge 67 commits into
oxc-project:mainfrom
armano2:feat/minimize-switch
Closed

feat(minifier): minimize switch statements#17523
armano2 wants to merge 67 commits into
oxc-project:mainfrom
armano2:feat/minimize-switch

Conversation

@armano2

@armano2 armano2 commented Dec 31, 2025

Copy link
Copy Markdown
Contributor

Phase 3: Advanced Optimizations

  • Switch statement optimization

changes:

  • converts 1-case and 2-case switches into more compact if or if/else statements when safe.
  • removes redundant empty case suffixes at the end of a switch.
  • removes empty switches (preserving the discriminant if it has side effects) and redundant trailing break statements.

note:

  • i left couple of todo's for later additions, that could add additional optimizations, i can remove them if needed
  • all packages seem to be affected due to napi last break removal from switch cases

future improvements:

  • we could remove all empty cases if there is no default or when its empty as long as test has no side effects
  • in-lining of switches with constant discriminants
  • merge of identical consequent cases without side effects

fixes #17544

@github-actions github-actions Bot added A-minifier Area - Minifier C-enhancement Category - New feature or request labels Dec 31, 2025
@codspeed-hq

codspeed-hq Bot commented Dec 31, 2025

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 57 untouched benchmarks
⏩ 19 skipped benchmarks1


Comparing armano2:feat/minimize-switch (7b56e64) with main (ba65790)

Open in CodSpeed

Footnotes

  1. 19 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.

@github-actions github-actions Bot added A-linter Area - Linter A-parser Area - Parser A-cli Area - CLI A-linter-plugins Area - Linter JS plugins labels Dec 31, 2025
@armano2
armano2 marked this pull request as ready for review December 31, 2025 19:32
@armano2

armano2 commented Dec 31, 2025

Copy link
Copy Markdown
Contributor Author

i'm pretty bad at naming stuff, suggestions are welcome 😄

Comment thread tasks/minsize/minsize.snap Outdated
Dunqing pushed a commit that referenced this pull request Jul 14, 2026
…4511)

Add baseline test cases for switch statement optimization, extracted
from #17523
Dunqing pushed a commit that referenced this pull request Jul 15, 2026
Replaces empty switch statements with its discriminant as expression
statement

based on findings from #17523
@armano2
armano2 marked this pull request as draft July 15, 2026 22:57
Dunqing added a commit that referenced this pull request Jul 16, 2026
remove empty cases:
- before default or at end (if no default).
- only if they don't affect behavior.

refactored implementation from #17523 

ref #17544

---------

Co-authored-by: Dunqing <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins A-minifier Area - Minifier A-parser Area - Parser C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minifier: switch statement optimization

5 participants