Skip to content

feat(es/minifier): Optimize typeof x == "undefined" to typeof x > "u"#11367

Merged
kdy1 merged 3 commits intoswc-project:mainfrom
magic-akari:feat/minifier-typeof
Dec 12, 2025
Merged

feat(es/minifier): Optimize typeof x == "undefined" to typeof x > "u"#11367
kdy1 merged 3 commits intoswc-project:mainfrom
magic-akari:feat/minifier-typeof

Conversation

@magic-akari
Copy link
Copy Markdown
Member

Description:

Optimize typeof x == "undefined" comparisons to shorter forms:

  • typeof x == "undefined"typeof x > "u" (for undeclared variables)
  • typeof x != "undefined"typeof x < "u" (for undeclared variables)
  • typeof x == "undefined"void 0 === x (for declared variables)

The > "u" optimization works because "undefined" is the only typeof result greater than "u" in lexicographic order:

typeof result > "u"
"bigint" false
"boolean" false
"function" false
"number" false
"object" false
"string" false
"symbol" false
"undefined" true

Performance

There were performance concerns with string comparison vs equality check, but these have been or are being addressed

BREAKING CHANGE:

Related issue:

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Dec 12, 2025

🦋 Changeset detected

Latest commit: dba8dbb

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 12, 2025

Binary Sizes

File Size
swc.linux-x64-gnu.node 31M (31777224 bytes)

Commit: 4a5b705

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 12, 2025

CodSpeed Performance Report

Merging #11367 will not alter performance

Comparing magic-akari:feat/minifier-typeof (dba8dbb) with main (c4a5e79)1

Summary

✅ 138 untouched

Footnotes

  1. No successful run was found on main (a0b96d5) during the generation of this report, so c4a5e79 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@magic-akari magic-akari marked this pull request as ready for review December 12, 2025 10:54
@magic-akari magic-akari requested review from a team as code owners December 12, 2025 10:54
Copilot AI review requested due to automatic review settings December 12, 2025 10:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes typeof x == "undefined" comparisons in the minifier to reduce output size. The optimization transforms:

  • typeof x == "undefined"typeof x > "u" (undeclared variables)
  • typeof x != "undefined"typeof x < "u" (undeclared variables)
  • typeof x == "undefined"void 0 === x (declared variables)

The > "u" optimization leverages the fact that "undefined" is the only typeof result lexicographically greater than "u".

Reviewed changes

Copilot reviewed 90 out of 91 changed files in this pull request and generated no comments.

File Description
bools.rs Implements the typeof optimization logic with proper handling of declared/undeclared variables and operator reversal
Test output files Consistently apply the optimization across numerous test snapshots including terser, react-dom, vue, moment, jquery, etc.
Size snapshots Show modest size reductions from the optimization (e.g., backbone reduced from 18.33 to 18.29 KiB)

The implementation correctly distinguishes between declared and undeclared variables, properly handles reversed operands, and flips operators appropriately for inequality comparisons.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread TODO-negate-comparison-operators.md Outdated
@kdy1 kdy1 added this to the Planned milestone Dec 12, 2025
Copilot AI review requested due to automatic review settings December 12, 2025 23:47
@kdy1 kdy1 merged commit a5e144b into swc-project:main Dec 12, 2025
24 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 89 out of 90 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@magic-akari magic-akari deleted the feat/minifier-typeof branch December 13, 2025 01:16
@kdy1 kdy1 modified the milestones: Planned, 1.15.4 Dec 14, 2025
@swc-project swc-project locked as resolved and limited conversation to collaborators Jan 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants