enabling eslint's prefer-const rule
This conversation started here: https://github.com/nodejs/node/pull/3036#discussion_r40710823.
It seems there's an implicit rule of preferring const over var for variables that hold values that never change, which makes sense. It's been mentioned in https://github.com/nodejs/node/issues/1243, and seems to be enforced during code reviews.
However, it's not currently caught by our eslint setup. To paraphrase https://github.com/nodejs/node/pull/3036#discussion_r40710823, I'm not a big fan of having implicit coding style rules. It becomes frustrating for maintainers to enforce them every time, and for contributors because they don't have any tool to check that their code complies to the guidelines.
However, using the prefer-const rule does not apply to function-scoped variable declarations, so we would need to use the no-var eslint rule too and use let instead of var, which I assume would require a significant amount of work.
https://github.com/nodejs/node/issues/1243 mentions that let's V8's implementation had performance at some point.
My question thus is: is moving from var to let and enforcing const with eslint worth investigating now?
/cc @nodejs/tsc
@chrisdickinson
Given that the codebase will survive multiple versions of V8, how long do we expect to see the let perf hit survive?
We don't code for what will be optimized, but what is optimized. And I have no idea when it will be fixed.
How big is the perf hit, if we take it — do we hit those corner cases? If so, can we rework to avoid those corner cases and add lint rules around them in the meantime?
In for loops it is substantial, and I don't have a conclusive list of every case where this is applicable. As you can see from my test case, the optimizing compiler completely fails. Generating almost twice the number of instructions. This is enough to turn from an annoyance to a blocking issue.
Even if we don't hit a case today, and we don't know all cases where it may appear, what will happen if a change comes in that hits it after this lands?
I'm +1 on making everything const that can be, but not at the cost of forcing let down everyone's throat.
Question: why can't we prefer const to var where applicable instead of let?
Closing this as prefer-const was enabled in b0e7b362c2ffe91d785446f420629e52eb793508 and no-var was shot down.
@chrisdickinson
Given that the codebase will survive multiple versions of V8, how long do we expect to see the let perf hit survive?
We don't code for what will be optimized, but what is optimized. And I have no idea when it will be fixed.
How big is the perf hit, if we take it — do we hit those corner cases? If so, can we rework to avoid those corner cases and add lint rules around them in the meantime?
In for loops it is substantial, and I don't have a conclusive list of every case where this is applicable. As you can see from my test case, the optimizing compiler completely fails. Generating almost twice the number of instructions. This is enough to turn from an annoyance to a blocking issue.
Even if we don't hit a case today, and we don't know all cases where it may appear, what will happen if a change comes in that hits it after this lands?
I'm +1 on making everything const that can be, but not at the cost of forcing let down everyone's throat.
I know this is closed, and there may be a more relevant discussion (someone please link?) but my solution is:
// let is much faster than const in for..of loops
// eslint-disable-next-line prefer-const
we should ping folks on the V8 team to get an idea of why this is still an issue. it’s been several years now and this syntax isn’t exactly new. it might just be a matter of adding this case into some of the perf suites JS VM’s are running.
we should ping folks on the V8 team to get an idea of why this is still an issue. it’s been several years now and this syntax isn’t exactly new. it might just be a matter of adding this case into some of the perf suites JS VM’s are running.
It has been resolved for some time. Loops using let have comparable performance to loops using var.
It has been resolved for some time. Loops using let have comparable performance to loops using var.
what about const?
It has been resolved for some time. Loops using let have comparable performance to loops using var.
what about
const?
Doh! I did the "see what I expected to see and not what is actually there on the screen in front of me" thing. Sorry. I don't know the answer to that.
It has been resolved for some time. Loops using let have comparable performance to loops using var.
what about
const?Doh! I did the "see what I expected to see and not what is actually there on the screen in front of me" thing. Sorry. I don't know the answer to that.
I don't know what sort of credibility jsperf has with the team here, but I created a test earlier today that addresses this question:
https://jsperf.com/foreach-vs-for-of-performance-test/9
After my initial run, let was definitively faster. After testing multiple times, I see variance between test runs. Sometimes let is faster and sometimes const is faster. It may vary through the amount of processing within the loop?
making sure @rvagg sees this so that he can stop frowning at my aggressive const usage 😜
bleh, I'm just a slave to whatever standard tells me these days