[BUG] npm install spends ~0.8 s in a quadratic loop in metavuln-calculator's Advisory.load

Đang mở
#10,319 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, node.js
Lĩnh vực
cli, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Triage Requested
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-calculator 10.0.0 (lib/advisory.js lines 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
  1. A project that depends on [email protected], with typical dev dependencies (735 packages in all).
  2. Run npm install, then run it again; the second is a no-op.
  3. 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.

Ngôn ngữ chính
JavaScript
Star
10.3k
Fork
5k
Merge trung bình
4 ngày 11 giờ
Pull request đã merge (30 ngày)
26

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của npm/cli

Tất cả issue của npm/cli

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.