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

lib: prefer optional chaining #55045

Merged
merged 1 commit into from
Sep 24, 2024

Conversation

RedYetiDev
Copy link
Member

No description provided.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/loaders
  • @nodejs/net
  • @nodejs/startup
  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Sep 21, 2024
@lpinca
Copy link
Member

lpinca commented Sep 21, 2024

Why? I won't block, but this is a cosmetic change that does not add any value in my opinion. It only adds noise to git blame.

@RedYetiDev
Copy link
Member Author

RedYetiDev commented Sep 21, 2024

IIRC optional chaining is slightly faster.

Quick and dirty JSBench shows a ~0.3-1% improvement in ops/sec: https://jsben.ch/znvKH
image

@lpinca
Copy link
Member

lpinca commented Sep 21, 2024

The performance is the same:

const Benchmark = require('benchmark');

const suite = new Benchmark.Suite();

const testBad = {};

suite.add('Optional chaining', function () {
  let wow;

  if (testBad?.id === '123') {
    wow = 'ok';
  }

  return wow;
});

suite.add('Without optional chaining', function () {
  let wow;

  if (testBad && testBad.id && testBad.id === '123') {
    wow = 'ok';
  }

  return wow;
});

suite.on('cycle', function (event) {
  console.log(event.target.toString());
});

suite.on('complete', function () {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
});

suite.run({ async: true });
$ node index.js 
Optional chaining x 103,663,582 ops/sec ±6.25% (75 runs sampled)
Without optional chaining x 106,320,156 ops/sec ±5.14% (76 runs sampled)
Fastest is Without optional chaining,Optional chaining

@RedYetiDev
Copy link
Member Author

ahh interesting. In that case, then yes, this is purely cosmetic, but I still think that using optional chaining is more concise and readable than without, but if it adds too much noise, it's not a needed change, however I'd love to see some other opinions.

Copy link
Member

@panva panva left a comment

Choose a reason for hiding this comment

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

I won't block but i'd say this ought to be accompanied by a linter enforcement. And that's when this will be debated ;)

Copy link

codecov bot commented Sep 21, 2024

Codecov Report

Attention: Patch coverage is 93.33333% with 5 lines in your changes missing coverage. Please review.

Project coverage is 88.24%. Comparing base (8b8fc53) to head (dbf1046).
Report is 445 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/esm/translators.js 0.00% 2 Missing ⚠️
lib/_tls_wrap.js 50.00% 0 Missing and 1 partial ⚠️
lib/assert.js 66.66% 1 Missing ⚠️
lib/net.js 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #55045   +/-   ##
=======================================
  Coverage   88.23%   88.24%           
=======================================
  Files         652      652           
  Lines      183855   183896   +41     
  Branches    35856    35856           
=======================================
+ Hits       162227   162274   +47     
+ Misses      14909    14908    -1     
+ Partials     6719     6714    -5     
Files with missing lines Coverage Δ
lib/_http_agent.js 98.19% <100.00%> (ø)
lib/_http_client.js 97.96% <100.00%> (ø)
lib/_http_common.js 100.00% <100.00%> (ø)
lib/_http_incoming.js 99.33% <100.00%> (ø)
lib/_http_outgoing.js 95.05% <100.00%> (ø)
lib/_http_server.js 96.74% <100.00%> (+0.08%) ⬆️
lib/child_process.js 97.74% <100.00%> (-0.01%) ⬇️
lib/dgram.js 97.31% <100.00%> (ø)
lib/internal/bootstrap/switches/is_main_thread.js 95.91% <100.00%> (ø)
lib/internal/child_process.js 94.72% <100.00%> (ø)
... and 24 more

... and 50 files with indirect coverage changes

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 21, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 21, 2024
@nodejs-github-bot
Copy link
Collaborator

test/common/dns.js Show resolved Hide resolved
@lpinca
Copy link
Member

lpinca commented Sep 21, 2024

Going forward, has the project's stance on cosmetic changes changed? We've rejected dozens of patches like this in the past. I think that approving some while rejecting others is disrespectful to contributors who have theirs rejected.

@RedYetiDev RedYetiDev added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 21, 2024
Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

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

Going forward, has the project's stance on cosmetic changes changed?

Once I think about it, I think I agree with @lpinca. This needs to come with a eslint rule.

@RedYetiDev RedYetiDev removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 21, 2024
@RedYetiDev RedYetiDev force-pushed the prefer-optional-chaining branch 2 times, most recently from 831d63b to d8d9568 Compare September 21, 2024 19:42
@RedYetiDev
Copy link
Member Author

@anonrig I added a prefer-optional-chaining lint rule. Are you still blocking?

Copy link
Member

@mertcanaltin mertcanaltin left a comment

Choose a reason for hiding this comment

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

lgtm

@RedYetiDev RedYetiDev added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Sep 23, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 23, 2024
@nodejs-github-bot
Copy link
Collaborator

@atlowChemi
Copy link
Member

@anonrig Would you mind re-reviewing or dismissing the request changes, as your concerns were addressed?

@RedYetiDev RedYetiDev added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 24, 2024
@atlowChemi atlowChemi added the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 24, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 24, 2024
@nodejs-github-bot nodejs-github-bot merged commit 574f2dd into nodejs:main Sep 24, 2024
62 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 574f2dd

targos pushed a commit that referenced this pull request Oct 4, 2024
PR-URL: #55045
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
@aduh95 aduh95 mentioned this pull request Oct 9, 2024
louwers pushed a commit to louwers/node that referenced this pull request Nov 2, 2024
PR-URL: nodejs#55045
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
@marco-ippolito marco-ippolito added the dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. label Nov 16, 2024
tpoisseau pushed a commit to tpoisseau/node that referenced this pull request Nov 21, 2024
PR-URL: nodejs#55045
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.