Skip to content

fs.globSync: time grows quadratically with the number of brace expansions #67711

Open

Description

@gustavohrm

Version

v26.13.1

Platform

Windows 11 x64 (10.0.26300)

Subsystem

fs (fs.glob, fs.globSync)

What steps will reproduce the bug?

Save as repro.mjs and run node repro.mjs 15 2, then node repro.mjs 7 4. It globs a pattern of N brace groups with K alternatives each, such as {a,b}{a,b}{a,b}.html, in an empty temporary folder. It has no dependencies.

// Time fs.globSync on a pattern of N brace groups of K alternatives each, in an empty folder.
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brace-"));
const max = Number(process.argv[2] ?? 14);
const alts = Number(process.argv[3] ?? 2);
for (let n = 1; n <= max; n += 1) {
  const group = `{${Array.from({ length: alts }, (_, i) => String.fromCharCode(97 + i)).join(",")}}`;
  const pattern = group.repeat(n) + ".html";
  const start = performance.now();
  fs.globSync(pattern, { cwd: dir });
  console.log(`node ${process.version} groups=${n} alternatives=${alts} expansions=${alts ** n} ${(performance.now() - start).toFixed(1)} ms`);
}
fs.rmSync(dir, { recursive: true });

How often does it reproduce? Is there a required condition?

Every run. The only condition is a pattern whose braces expand to thousands of patterns. The folder can be empty.

What is the expected behavior? Why is that the expected behavior?

Time roughly proportional to the number of expanded patterns. With no files to match, each pattern should cost about the same however many there are.

What do you see instead?

The cost of each pattern grows with the number of patterns, so the total grows roughly quadratically:

groups=10 alternatives=2 expansions=1024     56.2 ms   (~55 µs per pattern)
groups=11 alternatives=2 expansions=2048    115.6 ms
groups=12 alternatives=2 expansions=4096    272.9 ms
groups=13 alternatives=2 expansions=8192    729.7 ms
groups=14 alternatives=2 expansions=16384  4660.6 ms
groups=15 alternatives=2 expansions=32768 28038.2 ms   (~856 µs per pattern)

groups=5 alternatives=4 expansions=1024     56.5 ms
groups=6 alternatives=4 expansions=4096    254.6 ms
groups=7 alternatives=4 expansions=16384  1777.6 ms

At the same expansion count (16,384), two-way groups are slower than four-way groups (4.7 s vs 1.8 s), so the cost also depends on how the groups nest, not only on the number of patterns. A CLI that passes a user's glob to fs.globSync looks hung on a pattern like this, with no error.

Additional information

Measured on Windows only. We now refuse globs that expand to more than 1,024 patterns before calling fs.globSync.

Activity

  1. gustavohrm commented on Nov 22, 2026

    @gustavohrm
    Author

    Root cause, from a CPU profile of the 16,384-expansion case on v26.13.1: most of the time is spent building the matcher, not walking the folder. lib/internal/fs/glob.js creates the matcher with optimizationLevel: 2. With that level, the vendored minimatch 10.2.6 runs secondPhasePreProcess, which compares every expanded pattern with every other one (partsMatch and secondPhasePreProcess are the top two entries by self time). That loop is the quadratic part. The rest is one lstat per pattern, which grows linearly.

    This is already fixed upstream:

    • minimatch 11.0.5 removed the pairwise compare (isaacs/minimatch@70f442df, "dedupe level 2 patterns without a pairwise compare"). The same loop is the quadratic half of GHSA-g3hv-75wg-m249 (affected: >= 7.0.0, < 11.0.1), which reaches it through **/.. amplification rather than braces.
    • main no longer uses minimatch for fs.glob since "fs: implement glob natively". I have not measured braces there.
    • v26.x and v24.x still vendor minimatch 10.2.6 and pass optimizationLevel: 2, so both release lines are affected.

    So the fix for the release lines is probably a minimatch update to 11.0.5 or later on v26.x and v24.x, or optimizationLevel: 1 in glob.js.

    Correction to the report: the claim that two-way groups are slower than four-way groups at the same count, so that nesting matters, doesn't hold up. The same 16,384-expansion input ranges from 2.2 to 5.7 s between runs, two-way vs four-way measured 3.3 s vs 2.8 s, and a single flat group of 8,192 alternatives costs the same as nested groups that expand to 8,192. The cost depends on the number of expansions only.

  2. added
    dependenciesPRs that add, update, or configure Node.js dependencies.
    v24.xIssues that can be reproduced on v24.x or PRs targeting the v24.x-staging branch.
    v26.xIssues that can be reproduced on v26.x or PRs targeting the v26.x-staging branch.
    on Nov 22, 2026
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

    dependenciesPRs that add, update, or configure Node.js dependencies.v24.xIssues that can be reproduced on v24.x or PRs targeting the v24.x-staging branch.v26.xIssues that can be reproduced on v26.x or PRs targeting the v26.x-staging branch.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions