Skip to content
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

doc: refine sample code on tokens of util/parseArgs #49263

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

imcotton
Copy link
Contributor

Reduce mutations / side-effects on the code snippet in docs.

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module. labels Aug 20, 2023
@imcotton imcotton force-pushed the docs-parseargs-tokens-sample branch from b3095ba to f16a077 Compare August 21, 2023 13:52
doc/api/util.md Outdated Show resolved Hide resolved
doc/api/util.md Outdated Show resolved Hide resolved
@shadowspawn
Copy link
Member

I don't have a strong opinion, just a reaction.

The example is about using tokens. The old example code does not destructure much, and you see the tokens array and the token elements getting obviously named and processed, like both lines of:

tokens
   .filter((token) => token.kind === 'option')

That might be a positive rather than a negative for the purposes of a tokens example.

(Disclaimer: I wrote the example.)

@imcotton
Copy link
Contributor Author

imcotton commented Aug 22, 2023

tokens
   .filter((token) => token.kind === 'option')

Thank you to bring this up, I totally agree to have the token named to increase readability especially for sake of documentation purpose, I will change it back soon. (Done)

However for case of .reduce(...) below, without destructuring would otherwise too verbose (5 places) to me even with documentation purpose in mind, would it be OK to leave it as, or consistency is more important here? (waiting feedback...)

@bakkot
Copy link

bakkot commented Aug 23, 2023

Personally I find this snippet significantly harder to follow than the original, which should be a high priority for documentation. So I'm -1 on this change (as a contributor to parseArgs, but not a node maintainer).

The original also has the advantage of being O(n) rather than O(n^2); if your concern is purely about best practices, you should definitely find a way to do this without a quadratic algorithm.

@imcotton
Copy link
Contributor Author

To my knowledge, the delete operator would causing v8 de-optimize on its hidden class and turning it into slow object, it also makes TypeScript yelling:

The operand of a 'delete' operator must be optional.   ts(2790)

Performance checklist:

  • How may times would parseArgs runs? once
  • How may args should one program be taking? less than 10
  • Have you done any measurement? no

are you looking for Premature Optimization?

@bakkot
Copy link

bakkot commented Aug 24, 2023

I'm not saying that it will actually be slow enough to matter in this instance. I'm saying that if you want to encourage good practices in general, you should not have a sample where there is a reduce which makes a copy of the whole structure, because that's quadratic.

If your concern is not about what general practices this code is fostering, I don't understand why you're suggesting a change at all. All the PR description says is "avoid mutations / side-effects", which is a principle some people like to follow. To me, "avoid quadratic algorithms" seems like a much more important principle.

@shadowspawn
Copy link
Member

For interest the PRs that led to tokens feature and docs are:

(Over 180 comments, but of course that was for the full feature and not just the example.)

@imcotton
Copy link
Contributor Author

you should not have a sample where there is a reduce which makes a copy of the whole structure, because that's quadratic.

To me, "avoid quadratic algorithms" seems like a much more important principle.

@bakkot
Out of curiosity, have you run the actual benchmark against two implementations yet?

@imcotton
Copy link
Contributor Author

imcotton commented Aug 24, 2023

Back to Dec 2018, the v8 team had this article: Speeding up spread elements, it's about massive performance improvement of spread on Arrays where require nothing changes from end user, I wander if one day the Object literals got similar treatment, does it shake up any stands on O(n^2)?

I don't believe there is no room for syntax like { foo, ...rest } or { ...rest, bar } to be optimized from internal engine, just like Arrays.

I don't have anything on this PR left, feel free to close as you please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants