Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2022

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Feb 11, 2022

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
@sbc100 sbc100 requested a review from kripken February 15, 2022 00:44
Copy link
Member

@kripken kripken left a comment

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.
@sbc100 sbc100 merged commit 1ebd4c8 into main Feb 15, 2022
@sbc100 sbc100 deleted the consistent_typeof branch February 15, 2022 20:31
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