Skip to content

refactor(path-compare): Improve documentation and reduce implementation code size #2854

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

Merged
merged 3 commits into from
Jun 13, 2025

Conversation

gibson042
Copy link
Contributor

Refs: #2787

Description

Applies suggestions from #2787 (review) .

@gibson042 gibson042 requested review from kriskowal and boneskull June 11, 2025 15:03
Comment on lines +55 to +56
const aStringLength = a.join('').length;
const bStringLength = b.join('').length;
Copy link
Member

Choose a reason for hiding this comment

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

I do not feel strongly about this: I agree that the reduce is not ideal, but I prefer a solution that improves both legibility and is lest wasteful with allocations. To that end, I’d prefer to keep this as-is or go the other direction: replace cumulativeLength’s internals with a C-style for loop and call that once on each of these lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand the concern, but in practice I just can't see those allocations at all... I think the internal allocation implied by strings.join('').length doesn't actually happen in V8, as observed by comparing output from the following:

$ esbench.mjs -h V8 \
  -s 'const arr = Array.from({ length: 1000 }, (_, i) => `element${i}`);' \
  join:'ret = []; for(let i = 0; i < arr.length; i++) ret.push( arr.slice(i).join("") );' \
  --dump \
  | node --heap-prof-dir="$(pwd)" --heap-prof --heap-prof-interval 100
join sync 0.04 ops/ms (24.5e+6 ns/op) after 123 1-count observations

$ esbench.mjs -h V8 \
  -s 'const arr = Array.from({ length: 1000 }, (_, i) => `element${i}`);' \
  join:'ret = []; for(let i = 0; i < arr.length; i++) ret.push( arr.slice(i).join("").length );' \
  --dump \
  | node --heap-prof-dir="$(pwd)" --heap-prof --heap-prof-interval 100
join sync 0.04 ops/ms (24.5e+6 ns/op) after 123 1-count observations

The first captures output from join and so requires string allocations (takeObservation claims 5 MB, dominated by join), while the second does not (takeObservation claims just 18 KB, a third of which is Array.from).

Copy link
Member

Choose a reason for hiding this comment

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

Is it really worth optimizing? Given where pathCompare is consumed, the bottleneck is I/O.

Copy link
Member

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

No blockers

Comment on lines +43 to +46
// Undefined compares greater than anything else.
if (a === undefined || b === undefined) {
// eslint-disable-next-line no-nested-ternary
return a === b ? 0 : a === undefined ? 1 : -1;
Copy link
Member

Choose a reason for hiding this comment

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

This rule exists because:

Nesting ternary expressions can make code more difficult to understand.

It's hard for me to disagree with that.

If we're going to disable it on a one-off basis (and again on L21 above), we should have a justification for doing so. Otherwise, it may be better to leave it for now, then propose we disable the rule because we feel it isn't buying us anything.

Ideally, I'd like a rule that demands an extra comment for each one-off disabled rule (much like ban-ts-comment configured with 'ts-expect-error': 'allow-with-description'). 😄 I bet someone solved this already...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I think we really want is a maximum ternary nesting depth of 1 rather than 0, to allow for precisely this kind of use in comparison functions ($equal ? 0 : $aLessThanB ? -1 : 1).

Comment on lines +55 to +56
const aStringLength = a.join('').length;
const bStringLength = b.join('').length;
Copy link
Member

Choose a reason for hiding this comment

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

Is it really worth optimizing? Given where pathCompare is consumed, the bottleneck is I/O.

@gibson042 gibson042 force-pushed the gibson-2787-followup branch from 0cd5e6b to 6969dc2 Compare June 13, 2025 19:42
@gibson042 gibson042 merged commit beee17b into master Jun 13, 2025
16 checks passed
@gibson042 gibson042 deleted the gibson-2787-followup branch June 13, 2025 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants