Skip to content

perf: avoid array allocations for single-field distinct checks - #156

Open
RomainLanz wants to merge 2 commits into
4.xfrom
perf/distinct-single-field
Open

RomainLanz wants to merge 2 commits into
4.xfrom
perf/distinct-single-field

Conversation

@RomainLanz

@RomainLanz RomainLanz commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

distinct('field') created and traversed a temporary values array for every item, even though it only needed one value.

This change adds a dedicated single-field path that reads the value directly. It keeps JSON.stringify([value]), so null handling, value types, objects, and serialization behavior remain unchanged. Composite distinct checks are not modified.

Benchmark

Measured on Node.js 24.21.0 with compiled validators reused across validations. The workload validates arrays of 256 objects with one distinct scalar field.

Metric Before After Improvement
Validation time 48.12 µs 42.74 µs 11.2%
Allocations per validation 142.65 kB 103.43 kB 27.5%
GC collections over 12,000 validations 479 366 23.6%
GC pause time 54.3 ms 41.3 ms 23.9%

Checks

  • npm test, 1,135 passed and 1 todo
  • npm run typecheck
  • ESLint
  • Prettier

RomainLanz and others added 2 commits September 14, 2026 12:45
Restore wrapping non-array fields in an array so JavaScript callers
passing a numeric field no longer crash with "keys is not iterable".
Also cover null handling for composite distinct fields, which lost
coverage after single fields moved to a dedicated path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants