-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
util: Use of let and const. Optimized array use. #4678
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
Conversation
lib/util.js
Outdated
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.
This could be declared with const.
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.
That is not a good idea.
- Semantics: creating a memory reference (variable or constant) that will for sure be modified should be declared as a variable, not a constant.
- Scope: by using let the variable objects only exists in the scope of the if statement, but that is not
truewhen using const
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.
- Semantics: creating a memory reference (variable or constant) that will for sure be modified should be declared as a variable, not a constant.
The value that a const object refers to is fair game to be changed. We already do this in other places throughout the codebase.
- Scope: by using let the variable objects only exists in the scope of the if statement, but that is not true when using const
const is also block scoped in strict mode, which all of our code is run in.
|
@danielsan can you perhaps show the performance improvement in a benchmark? |
3182a19 to
735d0bb
Compare
|
Benchmarks added @jbergstroem |
|
@danielsan I think the benchmark @jbergstroem was looking for was a benchmark that shows the speed increase of |
|
I'm going to run this through CI as-is just to make sure there aren't any surprising issues lurking. |
|
Minor linting complaint on CI: |
lib/util.js
Outdated
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.
let in this case is apparently not optimized by v8 just yet. /cc @bnoordhuis @ofrobots
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.
Is that conjecture or did --trace_opt --trace_deopt print out something?
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.
Conjecture. I've been hearing rumors that let in for loops is not yet fully optimized by v8. Is that rumor incorrect?
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.
Incorrecter with every upgrade. My rule of thumb is to prefer let/const and fall back to var only when the optimizer complains. It's easy enough to check.
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.
+1.. Good to know!
On Feb 20, 2016 7:41 AM, "Ben Noordhuis" notifications@github.com wrote:
In lib/util.js
#4678 (comment):if (typeof f !== 'string') {
- var objects = [];
- for (var index = 0; index < arguments.length; index++) {
objects.push(inspect(arguments[index]));- let objects = new Array(len);
- for (let i = len; i--;) {
Incorrecter with every upgrade. My rule of thumb is to prefer let/const
and fall back to var only when the optimizer complains. It's easy enough to
check.—
Reply to this email directly or view it on GitHub
https://github.com/nodejs/node/pull/4678/files#r53552240.
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.
That has already been changed.
|
changed from let to const on the declaration of objects variable/constant |
|
LGTM |
Because accessing properties is more expensive than accessing variables and they will be used in any case I moved the variables declared on lines 25 and 26 to lines 14 and 15. In this way the code avoids to access the same properties more than once. The push method is more expensive than array index attribution. Since the new array length is always known, the creation of the array with a predefined length for future positioning attribution is less expensive than using the push method. Benchmarks added proving that the new approach is in average 2 to 3 times faster than the original one.
|
Turns out using |
|
If we are going to be avoiding let in for loops project wide I think this should also include a lint rule, which in turn should be sent upstream to eslint to allow this best practice to be adopted in the ecosystem |
7da4fd4 to
c7066fb
Compare
|
@mscdex ... what do you think about this one? given the benchmarking you've done, is this PR something we'd want to avoid for now? |
|
@jasnell I haven't done any benchmarking around |
|
what are the benchmarks on v6? |
Because accessing properties is more expensive than accessing variables
I moved the variables declared on lines 25 and 26 to lines 14 and 15.
Thus the previous piece of code can take advantage of those.
The push method is more expensive than array index attribution.
Since the new array length is always known, the creation of the array
with a predefined length for future positioning attribution is less
expensive than using the push method.