Skip to content

Conversation

@danielsan
Copy link

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.

lib/util.js Outdated
Copy link
Contributor

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.

Copy link
Author

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.

  1. Semantics: creating a memory reference (variable or constant) that will for sure be modified should be declared as a variable, not a constant.
  2. Scope: by using let the variable objects only exists in the scope of the if statement, but that is not true when using const

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. 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.

  1. 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.

@jbergstroem
Copy link
Member

@danielsan can you perhaps show the performance improvement in a benchmark?

@jasnell jasnell added the util Issues and PRs related to the built-in util module. label Jan 14, 2016
@danielsan danielsan force-pushed the master branch 3 times, most recently from 3182a19 to 735d0bb Compare February 19, 2016 23:28
@danielsan
Copy link
Author

Benchmarks added @jbergstroem

@Trott
Copy link
Member

Trott commented Feb 19, 2016

@danielsan I think the benchmark @jbergstroem was looking for was a benchmark that shows the speed increase of util.format() rather than a generic push-vs.-index benchmark.

@Trott
Copy link
Member

Trott commented Feb 19, 2016

I'm going to run this through CI as-is just to make sure there aren't any surprising issues lurking.

CI: https://ci.nodejs.org/job/node-test-pull-request/1708/

@Trott
Copy link
Member

Trott commented Feb 19, 2016

Minor linting complaint on CI:

/usr/home/iojs/build/workspace/node-test-linter/lib/util.js
  18:9  error  'objects' is never modified, use 'const' instead  prefer-const

��� 1 problem (1 error, 0 warnings)

lib/util.js Outdated
Copy link
Member

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

Copy link
Member

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?

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Author

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.

@danielsan
Copy link
Author

changed from let to const on the declaration of objects variable/constant

@jasnell
Copy link
Member

jasnell commented Mar 2, 2016

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.
@jasnell
Copy link
Member

jasnell commented Mar 22, 2016

Turns out using let has a real performance cost currently. Can this be updated to remove the use of let in the for loops?

@MylesBorins
Copy link
Contributor

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

@estliberitas estliberitas force-pushed the master branch 2 times, most recently from 7da4fd4 to c7066fb Compare April 26, 2016 05:22
@jasnell
Copy link
Member

jasnell commented Apr 30, 2016

@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?

@mscdex
Copy link
Contributor

mscdex commented Apr 30, 2016

@jasnell I haven't done any benchmarking around let since v8 5.0 yet. I would suggest holding off on this for the time being until we can show that let no longer has performance issues.

@MylesBorins
Copy link
Contributor

what are the benchmarks on v6?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants