Skip to content

perf(utils): improve performance of numberToPos #20244

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 20, 2025

Conversation

yuzheng14
Copy link
Contributor

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.

result of bun

result of nodejs

result of deno

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 the 0-based column described in the Pos type comments.

type Pos = {
/** 1-based */
line: number
/** 0-based */
column: number
}

@yuzheng14 yuzheng14 marked this pull request as draft June 19, 2025 13:57
@yuzheng14 yuzheng14 marked this pull request as ready for review June 19, 2025 14:22
@yuzheng14
Copy link
Contributor Author

I noticed an inconsistency in the previous test snapshot: when receiving an offset of 8, it returns { line: 1, column: 9 } which appears incorrect.

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label Jun 20, 2025
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Thanks! The change looks good to me.
Would you add some tests for numberToPos below here so that we can catch the bug you found more easily?

describe('posToNumber', () => {
test('simple', () => {
const actual = posToNumber('a\nb', { line: 2, column: 0 })
expect(actual).toBe(2)
})
test('pass though pos', () => {
const actual = posToNumber('a\nb', 2)
expect(actual).toBe(2)
})
test('empty line', () => {
const actual = posToNumber('a\n\nb', { line: 3, column: 0 })
expect(actual).toBe(3)
})
test('out of range', () => {
const actual = posToNumber('a\nb', { line: 4, column: 0 })
expect(actual).toBe(4)
})
})

@yuzheng14 yuzheng14 requested a review from sapphi-red June 20, 2025 02:08
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Nice! It looks more readable too

@bluwy bluwy merged commit 3f46901 into vitejs:main Jun 20, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy