Skip to content

perf(version): cache parsed version tuples in compareVersions - #740

Merged
sonukapoor merged 11 commits into
OWASP:mainfrom
luojiyin1987:perf/compare-versions-cache
Jun 23, 2026
Merged

sonukapoor merged 11 commits into
OWASP:mainfrom
luojiyin1987:perf/compare-versions-cache

Conversation

@luojiyin1987

Copy link
Copy Markdown
Collaborator

Closes #739

Summary

compareVersions is on the hot path: candidate sorting, range matching, and fix validation all call it repeatedly. Each call splits strings and maps them to numbers. For a packument like lodash with 100+ versions, sorting calls compareVersions ~700 times, re-parsing the same strings each time.

Changes

  • Added a Map<string, readonly (number|string)[]> cache in src/utils/version.ts
  • Extracted parsing into parseVersionTuple that checks the cache before splitting
  • Used Object.freeze to prevent mutation of cached arrays
  • Return type is readonly for TypeScript-level enforcement

Verification

  • TypeScript: no errors
  • 46/46 tests pass

@sonukapoor sonukapoor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The implementation is clean - Object.freeze on cached arrays is a nice touch. Two small things before this is good to go.

Comment thread src/utils/version.ts
Comment thread tests/helpers.test.ts Outdated
@luojiyin1987
luojiyin1987 requested a review from sonukapoor June 23, 2026 00:25
@sonukapoor

Copy link
Copy Markdown
Collaborator

Branch is one commit behind main after #742 merged. Could you rebase when you get a chance? No conflicts expected — the files don't overlap.

@sonukapoor
sonukapoor merged commit c85e690 into OWASP:main Jun 23, 2026
6 checks passed
@sonukapoor

Copy link
Copy Markdown
Collaborator

Merged — thank you @luojiyin1987!

@luojiyin1987

Copy link
Copy Markdown
Collaborator Author

Branch is one commit behind main after #742 merged. Could you rebase when you get a chance? No conflicts expected — the files don't overlap.

something I need to do ?

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.

perf(version): cache parsed version tuples in compareVersions

2 participants