Skip to content

perf: avoid redundant work in redactSensitiveHeaders on each call#113

Merged
melikhov-dev merged 1 commit intomainfrom
fix/redact-sensitive-headers-perf
Mar 2, 2026
Merged

perf: avoid redundant work in redactSensitiveHeaders on each call#113
melikhov-dev merged 1 commit intomainfrom
fix/redact-sensitive-headers-perf

Conversation

@melikhov-dev
Copy link
Contributor

Problem

prepareSensitiveHeadersRedacter returns a closure (redactSensitiveHeaders) that is called on every incoming request. Inside that closure, two expensive operations were being repeated unnecessarily on each invocation:

  1. headersWithSensitiveUrls.map(name => name.toLowerCase()) — rebuilds the lowercased array every call
  2. prepareSensitiveKeysRedacter(sensitiveHeaders) — creates a new redacter function with a new closure every call

Both inputs (sensitiveHeaders, headersWithSensitiveUrls) are fixed at the time prepareSensitiveHeadersRedacter is called and never change afterward.

Fix

Move both computations to the outer factory function scope, so they run once at initialization rather than on every request.

Test plan

  • All 67 existing unit tests pass (npm test)
  • Behaviour is identical — only timing of allocation changes, not results

Move headersWithSensitiveUrlsLowered and redactSensitiveKeys outside
the inner closure so they are computed once at initialization time
rather than on every invocation of redactSensitiveHeaders.
@melikhov-dev melikhov-dev requested a review from resure as a code owner March 2, 2026 08:22
@melikhov-dev melikhov-dev merged commit 7a4c58b into main Mar 2, 2026
5 checks passed
@melikhov-dev melikhov-dev deleted the fix/redact-sensitive-headers-perf branch March 2, 2026 08:58
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