-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
lib: prefer optional chaining #55045
Conversation
Review requested:
|
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. |
IIRC optional chaining is slightly faster. Quick and dirty JSBench shows a ~0.3-1% improvement in ops/sec: https://jsben.ch/znvKH |
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 });
|
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. |
There was a problem hiding this 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 ;)
Codecov ReportAttention: Patch coverage is
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
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. |
There was a problem hiding this 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.
831d63b
to
d8d9568
Compare
@anonrig I added a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
d8d9568
to
dbf1046
Compare
@anonrig Would you mind re-reviewing or dismissing the request changes, as your concerns were addressed? |
Landed in 574f2dd |
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>
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>
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>
No description provided.