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. |
panva
left a comment
There was a problem hiding this comment.
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
|
|
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. |
831d63b to
d8d9568
Compare
|
@anonrig I added a |
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.