Skip to content

perf(isEmail, isFQDN, normalizeEmail): up to 2x faster by skipping the default-options copy - #2932

Open
deadstrobe5 wants to merge 1 commit into
validatorjs:masterfrom
deadstrobe5:perf/skip-default-options-copy
Open

deadstrobe5 wants to merge 1 commit into
validatorjs:masterfrom
deadstrobe5:perf/skip-default-options-copy

Conversation

@deadstrobe5

Copy link
Copy Markdown

When called without options, isEmail, isFQDN and normalizeEmail ran merge(undefined, defaults), which allocates a new object and copies every default into it on each call. None of these functions mutate options or pass it on to code that does (isEmail builds a fresh object for its isFQDN call), so they can read the module-level defaults directly. Calls that pass options still go through merge unchanged, and any falsy options value previously produced exactly a copy of the defaults, so results are the same.

Benchmark (Node 26, Apple M5, A/B interleaved in one process, 20 rounds × 100 ms, median ratio; an A/A control gave 1.00 with ±2–4% noise):

Function Before After Speedup
isEmail 433 ns 339 ns 1.27x
isFQDN 208 ns 138 ns 1.50x
normalizeEmail 313 ns 150 ns 2.06x

isURL is unchanged on purpose: it passes its own options object to isFQDN, so it keeps the current path.

A differential test of ~186,000 inputs and option combinations (calls with and without options interleaved in one process) against the previous build found no differing results. npm test passes (323 tests, 100% coverage on the changed files) and eslint is clean.

Checklist

  • PR contains only changes related; no stray files, etc.
  • README updated (where applicable)
  • Tests written (where applicable)
  • References provided in PR (where applicable)

@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9ff3424) to head (d404b3f).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2932   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2599      2599           
  Branches       658       660    +2     
=========================================
  Hits          2599      2599           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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