perf(utils): improve performance of numberToPos
#20244
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR optimizes the
numberToPos
function, achieving 1.66x-2.27x performance improvement across different JavaScript runtimes. The refactored implementation also provides clearer and more maintainable code.Benchmarks
The code of benchmarks is shown at Stackblitz.
Performance was measured using a 9,179-character test file across multiple runtimes locally.
Behavior Changes
The current implementation return positions like
{ line: /* previous line */, column: /* line length including newline */ }
. The new implementation consistently return the start position of the next line like{ line: /* exact the line */, column: 0 }
, which better aligns with the0-based
column described in thePos
type comments.vite/packages/vite/src/node/utils.ts
Lines 471 to 476 in 657c2fa