Skip to content

perf: speed up index generation sorting - #106

Open
RomainLanz wants to merge 1 commit into
8.xfrom
perf/reuse-index-sort-collator
Open

perf: speed up index generation sorting#106
RomainLanz wants to merge 1 commit into
8.xfrom
perf/reuse-index-sort-collator

Conversation

@RomainLanz

Copy link
Copy Markdown
Member

Hey! 👋

VirtualFileSystem.scan currently uses naturalSort, which calls localeCompare with collation options for every comparison. Sorting becomes the main CPU cost when generating indexes with hundreds or thousands of files.

This PR creates one Intl.Collator with the same options and reuses its bound comparator. It preserves the existing natural, case-insensitive, and accent-insensitive order. A focused test covers numbers, leading zeros, casing, accents, and nested directories.

Benchmark

Node.js 24.20.0 using the public IndexGenerator API, with 12 independent before/after process pairs and alternating execution order.

Files Before After Change
10 0.750 ms 0.628 ms -16.3%
25 1.877 ms 1.435 ms -23.5%
50 3.266 ms 2.150 ms -34.2%
100 5.158 ms 2.756 ms -46.6%
1,000 34.575 ms 10.908 ms -68.5%
5,000 184.340 ms 51.217 ms -72.2%

At 1,000 files, the CPU profile dropped from 2,120 ms to 673 ms across 60 generations.

The old and new comparators produced identical results across 4,206,601 comparisons and 100 shuffled stable sorts. A generated 1,000-file index was also byte-identical.

Validation

  • 262 tests passed
  • Typecheck passed
  • ESLint passed

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.

1 participant