Skip to content

perf(usage): stripComments makes import extraction ~11x slower #1120

Description

@sonukapoor

Note: this is an in-house item already being handled by the maintainer - not open for contribution. Filed for tracking only.

Measured while reviewing #1114, which adds a stripComments pass to scanAllImports so JSDoc and commented-out imports stop counting as usage. The pass is correct and worth keeping. It is also about 11x slower than what it replaces.

Measured

src/ plus tests/ in this repo, 271 files, 1.9 MB, averaged over 5 passes:

read only (I/O) :  3.2 ms/pass
old extract     :  1.2 ms/pass
new extract     : 13.0 ms/pass

So the scanning phase goes from roughly 4.4 ms to 16.2 ms, with the extraction step itself about 11x. At the 5,000-file cap that is roughly 240 ms against 22 ms.

Cause

stripComments builds its result with per-character out += ch over the whole file, materialising a second copy of every source file. The pre-filter in scanAllImports (content.includes("import") || "require" || "export") admits essentially every source file, so almost nothing is skipped.

Options

Accumulate chunks in an array and join("") at the end, or compute comment ranges once and filter regex matches by index rather than building a stripped copy at all. The second avoids the allocation entirely and is probably the better shape, since the caller only needs match positions.

Priority

Not urgent. Absolute numbers are small and correctness matters more than the milliseconds here. Filed because #837 (CLI performance audit) is open in P1 and this is a known, measured, cheap-to-fix regression that should be folded into that work rather than rediscovered later.

Should land after #1114 merges, since it modifies code that PR introduces.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestin-houseMaintainer-handled internal work - not open for contribution

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions