perf: prefer Object.keys over Object.entries in hot header/query loops - #96
Conversation
@standard-server/aws-lambda
@standard-server/core
@standard-server/fastify
@standard-server/fetch
@standard-server/node
@standard-server/peer
@standard-server/shared
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This is a single-commit mechanical refactor that swaps Object.entries destructuring for Object.keys plus an indexed read in the hot header/query loops. I read the full diff, the surrounding functions, the StandardHeaders/AWS event types, and ran typecheck and the affected package tests.
- Header/query loops in
aws-lambda—toStandardHeaders,getEventHeader,toLambdaHeaders, andtoStandardUrlnow iterate keys and read values by index. Allundefined/empty-array guards are preserved, so null-prototype andundefined-valued header semantics are unchanged. mergeStandardHeaders—bValueis now read only after the!Object.hasOwn(a, key)early-continue, which matches the original result while skipping a lookup for keys absent froma.- Response/header emit loops —
node,fastify, andfetchadapters converted the same way; the "don't passundefined" guards that keep fastify from turning them into empty strings are intact.
Behavior is equivalent: Object.keys and Object.entries enumerate the same own enumerable string keys in the same order, the sources are plain records (no getters/proxies) that are not mutated mid-iteration, and the entries tuple was never used as a snapshot. noUncheckedIndexedAccess is enabled (tsconfig.base.json), which is why the indexed reads still typecheck as T | undefined and the guards remain meaningful. Verified locally: pnpm run type:check exit 0, and the core/aws-lambda/fetch/node/fastify vitest run passed 374/374. Existing tests already exercise every changed function, so no new coverage is warranted.
DeepSeek Flash (default — pick a model for stronger reviews) | 𝕏

Replaces
Object.entrieswithObject.keysplus an indexed read in the header and query-string loops across core, fetch, node, fastify, and aws-lambda. This removes one[key, value]tuple allocation per entry on every request and response header pass.Performance
mergeStandardHeadersskips theb[key]lookup for keys not present ina.Type safety
!assertions added.noUncheckedIndexedAccessalready typesobj[key]asT | undefined, and every loop already guarded againstundefined.Testing
pnpm run type:checkpasses for all packages.pnpm run testpasses (1128 vitest tests plus the bun and deno suites).