Skip to content

perf: skip per-character loop for safe long strings - #95

Open
mcollina wants to merge 1 commit into
anonrig:mainfrom
mcollina:perf/optimize-encodestring
Open

mcollina wants to merge 1 commit into
anonrig:mainfrom
mcollina:perf/optimize-encodestring

Conversation

@mcollina

@mcollina mcollina commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Speed up stringify() for keys and values that contain no characters requiring escaping. encodeString now first scans for the first escapable character with a native regex when the string is long enough, and returns it unchanged when it is already safe, instead of running the per-character scanning loop. Short strings keep the original tight loop because regex call overhead dominates there.

Measured against the previous implementation, this is neutral on the existing short-string benchmark and ~9% faster on realistic long-value workloads; a 40+ character safe string encodes roughly 2x faster. All tests pass and 20k fuzz cases match node:querystring.


Note

Low Risk
Localized performance change to URL encoding with intended identical output; no security or API surface changes.

Overview
Speeds up query-string encoding when keys or values are long and already “safe” (no characters that need percent-encoding).

encodeString now uses a native regex pre-scan for inputs ≥ 16 characters to detect the first character that must be escaped. If none are found, it returns the input unchanged instead of walking every code unit. When escaping is needed, it only runs the existing per-character loop from that point, prefixing the safe leading slice. Shorter strings still use the original tight loop only, because regex overhead would outweigh the benefit.

Reviewed by Cursor Bugbot for commit 6bb6c67. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.23%. Comparing base (9fe75ff) to head (6bb6c67).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #95      +/-   ##
==========================================
+ Coverage   69.31%   70.23%   +0.91%     
==========================================
  Files           7        7              
  Lines         453      467      +14     
  Branches       74       78       +4     
==========================================
+ Hits          314      328      +14     
  Misses        136      136              
  Partials        3        3              

☔ 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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 6bb6c67. Configure here.

1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 112 - 127
]);

const needEscape = /[^A-Za-z0-9!\-.~'()*]/;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex omits underscore from safe set

Medium Severity

The needEscape regex omits _, which noEscape treats as safe. Long strings containing underscores therefore miss the unchanged-return path and allocate a prefix plus a concatenation even when every character is already safe.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6bb6c67. Configure here.

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