Skip to content
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

Doesn't this encourage inefficient coding? #2

Open
brad4d opened this issue Jul 6, 2023 · 2 comments
Open

Doesn't this encourage inefficient coding? #2

brad4d opened this issue Jul 6, 2023 · 2 comments

Comments

@brad4d
Copy link

brad4d commented Jul 6, 2023

One of the examples in this gist is:


 if (node) {
   node.kind = "init";
   node.type = "Property";
}

could be rewritten as

node?.kind = "init";
node?.type = "Property";

This is performing the same test twice at runtime.
The original was more efficient.

@nicolo-ribaudo
Copy link
Member

This does not encourage neither more nor less efficient coding. In the example you quotes, it's true that there is one more nullish check but, in the case of a non-nullish node, there is one less variable lookup.

In some other examples, such as if (element.node) element.node.decorators = null;, using element.node?.decorators = null avoids reading element.node twice.

@MadProbe
Copy link

Also JS engines could be smart and hoist the same conditions if it's safe to do so, so this is not very concerning of an issue.

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

No branches or pull requests

3 participants