Skip to content

perf: prefer Object.keys over Object.entries in hot header/query loops - #96

Merged
dinwwwh merged 1 commit into
mainfrom
claude/object-keys-perf-332c10
Sep 18, 2026
Merged

dinwwwh merged 1 commit into
mainfrom
claude/object-keys-perf-332c10

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Sep 18, 2026

Copy link
Copy Markdown
Member

Replaces Object.entries with Object.keys plus 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

  • No per-entry array allocation when converting or merging headers and query parameters.
  • mergeStandardHeaders skips the b[key] lookup for keys not present in a.

Type safety

  • No ! assertions added. noUncheckedIndexedAccess already types obj[key] as T | undefined, and every loop already guarded against undefined.

Testing

  • pnpm run type:check passes for all packages.
  • pnpm run test passes (1128 vitest tests plus the bun and deno suites).

@pkg-pr-new

pkg-pr-new Bot commented Sep 18, 2026

Copy link
Copy Markdown
@standard-server/aws-lambda

npm i https://pkg.pr.new/@standard-server/aws-lambda@96

@standard-server/core

npm i https://pkg.pr.new/@standard-server/core@96

@standard-server/fastify

npm i https://pkg.pr.new/@standard-server/fastify@96

@standard-server/fetch

npm i https://pkg.pr.new/@standard-server/fetch@96

@standard-server/node

npm i https://pkg.pr.new/@standard-server/node@96

@standard-server/peer

npm i https://pkg.pr.new/@standard-server/peer@96

@standard-server/shared

npm i https://pkg.pr.new/@standard-server/shared@96

commit: 8867841

@codecov

codecov Bot commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 108 skipped benchmarks1


Comparing claude/object-keys-perf-332c10 (8867841) with main (466adb3)2

Open in CodSpeed

Footnotes

  1. 108 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (eeda6b7) during the generation of this report, so 466adb3 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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, and toStandardUrl now iterate keys and read values by index. All undefined/empty-array guards are preserved, so null-prototype and undefined-valued header semantics are unchanged.
  • mergeStandardHeaders — bValue is now read only after the !Object.hasOwn(a, key) early-continue, which matches the original result while skipping a lookup for keys absent from a.
  • Response/header emit loops — node, fastify, and fetch adapters converted the same way; the "don't pass undefined" 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.

Pullfrog  | View workflow run | Using DeepSeek Flash (default — pick a model for stronger reviews) | 𝕏

@dinwwwh
dinwwwh merged commit e92aea4 into main Sep 18, 2026
11 checks passed
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