Skip to content

http: trim off brackets from IPv6 addresses with string operations#59420

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
pckrishnadas88:net/proxy-optimize-ipv6-trim
Aug 20, 2025
Merged

http: trim off brackets from IPv6 addresses with string operations#59420
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
pckrishnadas88:net/proxy-optimize-ipv6-trim

Conversation

@pckrishnadas88

@pckrishnadas88 pckrishnadas88 commented Aug 9, 2025

Copy link
Copy Markdown
Contributor

Replace regex-based hostname normalization with direct string operations, selecting the most performant of three tested approaches:

Performance Hierarchy (http/proxy-config-ipv6-trim.js):

  1. stringOp (chosen implementation):
    • IPv6: 267,922,975 ops/sec (15.5x faster than regex)
    • IPv4: 354,683,255 ops/sec (9.3x faster than regex)
  2. normalize: 205-337M ops/sec
  3. regex: 17-38M ops/sec

Before (regex):

this.hostname = hostname.replace(/^\[|\]$/g, '');
// ~17M ops/sec (IPv6) | ~38M ops/sec (IPv4)

After (optimized):

this.hostname = this.#normalizeHostname(hostname); // Trim off the brackets from IPv6 addresses.
// Private helper method for performance optimization
#normalizeHostname(hostname) {
    return hostname.startsWith('[') && hostname.endsWith(']') ?
      hostname.slice(1, -1) :
      hostname;
}
// ~267M ops/sec (IPv6) | ~354M ops/sec (IPv4)

Implementation Details:

  • Uses direct startsWith('[') && endsWith(']') checks (stringOp)
  • Chosen over alternative implementations after benchmarking
  • Maintains 1:1 behavior with existing regex implementation
  • More efficient for proxy servers handling high connection volumes

Behavior remains identical while being significantly faster.

Verification:

  1. Run benchmarks:
    ./node benchmark/http/proxy-config-ipv6-trim.js

Benefits:

  • Faster proxy configuration for IPv6/IPv4 addresses
  • No behavior changes - purely an optimization
  • Better performance for HTTP servers handling many connections.

Note: This is unrelated to #59375 (approved, awaiting merge).

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-requested-v22.x PRs awaiting manual backport to the v22.x-staging branch. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.