[BUG] npm install spends ~0.8 s in a quadratic loop in metavuln-calculator's Advisory.load
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 68/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- javascript, node.js
- 领域
- cli, performance
调研方向
Start with @npmcli/metavuln-calculator/lib/advisory.js, especially Advisory.load around the reported lines 74 and 108–140, and inspect how version membership and removal are checked. Reproduce with the provided Next.js project and CPU profile, then compare audit results and timing after the change. Done means the audit work scales linearly with package versions while preserving vulnerability results.
由索引模型根据 Issue 内容生成。
描述
Is there an existing issue for this?
- I have searched the existing issues
This issue exists in the latest npm version
- I measured on npm 11.2.0. The same code is in npm 12.2.3, which bundles
@npmcli/metavuln-calculator10.0.0 (lib/advisory.jslines 114, 117, 124 and 139).
This is not just a request to bump a dependency for a CVE
- This is not solely a request to bump a dependency for a CVE
Current Behavior
A no-op npm install in a Next.js project (next 15.5.4 with typical dev dependencies, 735 packages) takes 2.6 s, against 0.55 s with --no-audit --no-fund. Most of the difference is CPU in the audit step, and about 0.8 s of it is a loop that is quadratic in the number of versions of a package.
With node --cpu-prof, 677 ms of self time is in @npmcli/metavuln-calculator/lib/index.js:50 (_calculate, with Advisory.load inlined), and Advisory.load (advisory.js:74) takes 401 ms inclusive. Advisory.load loops over every version of the package and calls this.versions.includes(v) and pakuVersions.includes(v), then filters with versionsRemoved.includes(v). That is O(versions²) for each advisory. In this tree next has 4,041 versions and 37 advisories.
Using Sets for the lookups keeps the same results:
--- a/lib/advisory.js
+++ b/lib/advisory.js
@@ -108,20 +108,24 @@
const pakuVersions = Object.keys(packument.versions || {})
const allVersions = new Set([...pakuVersions, ...this.versions])
+ const pakuSet = new Set(pakuVersions)
+ const haveSet = new Set(this.versions)
const versionsAdded = []
const versionsRemoved = []
for (const v of allVersions) {
- if (!this.versions.includes(v)) {
+ if (!haveSet.has(v)) {
versionsAdded.push(v)
this.versions.push(v)
- } else if (!pakuVersions.includes(v)) {
+ haveSet.add(v)
+ } else if (!pakuSet.has(v)) {
versionsRemoved.push(v)
}
}
+ const removedSet = new Set(versionsRemoved)
// strip out any removed versions from our lists, and sort by semver
this.versions = semver.sort(this.versions.filter(v =>
- !versionsRemoved.includes(v)), semverOpt)
+ !removedSet.has(v)), semverOpt)
// if no changes, then just return what we got from cache
// versions added or removed always means we changed
@@ -136,7 +140,7 @@
// otherwise, only recheck the new ones.
this.vulnerableVersions = !unchanged ? []
: semver.sort(this.vulnerableVersions.filter(v =>
- !versionsRemoved.includes(v)), semverOpt)
+ !removedSet.has(v)), semverOpt)
if (unchanged && !versionsAdded.length && !versionsRemoved.length) {
// nothing added or removed, nothing to do here. use the cached copy.
A no-op npm install over 7 alternating rounds went from a median of 2,531 ms to 1,757 ms (−31%). The ranges don't overlap: 2,437–2,608 ms against 1,718–1,795 ms.
npm audit --json reports the same vulnerabilities with and without the change. Its effects and fixAvailable for jest-cli and @jest/core change between runs, but they change the same way between two runs of unpatched npm (as in #10313).
A second, smaller cost: semver.sort over the 4,041 versions for each advisory takes about 200 ms.
Expected Behavior
The audit step's cost grows linearly with the number of versions.
Steps To Reproduce
- A project that depends on
[email protected], with typical dev dependencies (735 packages in all). - Run
npm install, then run it again; the second is a no-op. - Compare its time with
npm install --no-audit --no-fund, or profile it:node --cpu-prof $(which npm) install.
Environment
- npm: 11.2.0 (the code is unchanged in 12.2.3)
- Node.js: 22.14.0
- OS Name: macOS 14.2.1 (Apple silicon)
- System Model Name: Apple silicon Mac
- npm config: defaults
Measured with wits. I used an AI assistant to help investigate and write this up; the numbers are from the runs described above.
- 主要语言
- JavaScript
- 星标
- 10.3k
- 派生
- 5k
- 平均合并
- 4 天 11 小时
- 30 天内合并 PR
- 26
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
npm/cli 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 2 天内回复
-
[BUG] scripts/dependency-graph.js splits npm-cli-repos.txt on os.EOL, so DEPENDENCIES.md depends on the checkout's line endings可能已有人在做 @saruk-vedant 于 7 天前认领。 未关闭Bug Needs Triage Triage Requested
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 2 天内回复
-
[BUG] npm pkg delete removes the first array item for a nonexistent property可能已有人在做 @drawliin 于 42 天前认领。 未关闭cmd:delete Priority 3
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 2 天内回复
-
create node pr: Node.js 20 deprecation & `DEP0040` warnings logged可能重新可做 关联的 PR 已关闭且未合并。 未关闭Engineering Priority 1
难度 1/5 1 小时以内 新手友好度 80/100
维护者通常 2 天内回复
-
`npm audit signatures` does not report how many packages it skipped for want of registry keys可能已有人在做 @greenlincoln05 于 63 天前认领。 未关闭Priority 3
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 2 天内回复
相似的 Issue
-
documentation
难度 1/5 1 小时以内 新手友好度 88/100
githubnext/gh-aw-workshop#6332 ·
维护者通常 1 天内回复
-
App Designer bug
难度 2/5 1-3 小时 新手友好度 78/100
avniproject/avni-webapp#1858 ·
-
bug
难度 1/5 1 小时以内 新手友好度 96/100
hiero-ledger/hiero-sdk-js#4834 ·
维护者通常 1 天内回复
-
[组件名称] 描述问题的标题未关闭🐞 bug 🧐 unconfirmed
难度 2/5 1-3 小时 新手友好度 68/100
Tencent/tdesign-miniprogram#4744 · 1 条评论 ·
维护者通常 1 天内回复
-
Y-axis tooltip hides near the bottom of the plot while the y crosshair stays visible可能已有人在做 @lazerg 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 82/100
apexcharts/apexcharts.js#5449 ·
维护者通常 3 天内回复