Skip to content

Consistent use of typeof operator in JS library code. NFC - #16261

Merged
sbc100 merged 1 commit into
mainfrom
consistent_typeof
Feb 15, 2022
Merged

sbc100 merged 1 commit into
mainfrom
consistent_typeof

Conversation

@sbc100

@sbc100 sbc100 commented Feb 11, 2022 •

Copy link
Copy Markdown
Collaborator

I noticed that in some places we were using equality checks
(==/!=) and in others we were using identity checks (===/!==).

In general identity checks are safer, but since typeof is known
to always returns a string and the RHS of these checks is also
always a string we can choose to always prefer != and ==
over !== and === for the sake of code size (sad but true,
we care that much about each byte of output).

Perhaps a better solution would be to teach closure compiler
how to remove the extra = when its know that both sides of
the operator are the same type already.. but that would be a
much larger change.

@sbc100 sbc100 changed the title Consisten use of typeof operator in JS library code. NFC Consistent use of typeof operator in JS library code. NFC Feb 11, 2022
@sbc100
sbc100 requested a review from kripken February 11, 2022 22:46
@sbc100
sbc100 force-pushed the consistent_typeof branch 3 times, most recently from 95c6783 to 9cf7c2f Compare February 12, 2022 00:55
Comment thread src/library_browser.js Outdated
@sbc100
sbc100 requested a review from kripken February 15, 2022 00:44

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

Wow, we have a lot of these... including one in a switch. I guess that's what we need in a dynamic language like JS...

I noticed that in some places we were using equality checks (==/!=) and
in others we were using identity checks (===/!==).

In general identity checks are safer, but since typeof is known to
always returns a string and the RHS of these checks is also always a
string we can choose to always prefer `!=` and `==` over `!==` and `===`
for the sake of code size (sad but true, we care that much about each
  byte of output).

Perhaps a better solution would be to teach closure compiler how to
remove the extra `=` when its know that both sides of the operator are
the same type already.. but that would be a much larger change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants