Skip to content

Comments

perf(aws-lambda): optimize header parsing to reduce micro-allocations#4762

Open
fcarvajalbrown wants to merge 1 commit intohonojs:mainfrom
fcarvajalbrown:main
Open

perf(aws-lambda): optimize header parsing to reduce micro-allocations#4762
fcarvajalbrown wants to merge 1 commit intohonojs:mainfrom
fcarvajalbrown:main

Conversation

@fcarvajalbrown
Copy link

Description

This PR optimizes the getHeaders method in the AWS Lambda adapter. By switching from Object.entries() to a for...in loop with a safety check, we avoid the overhead of creating intermediate arrays for every request.
In high-throughput serverless environments, reducing these "micro-allocations" significantly lowers Garbage Collection (GC) pressure and minimizes latency jitter.

Performance Details

benchmark avg (min … max) p75 / p99


Current (Object.entries) 1.26 µs/iter 1.20 µs / 2.30 µs
Optimized (for...in) 851.85 ns/iter 860.13 ns / 954.57 ns

Summary:
Optimized (for...in) is 1.48x faster than Current (Object.entries)

Testing

[x] Benchmark Validation: Verified with mitata (results above).

[x] Functional Testing: Ran the full Hono test suite.

[x] Runtime Verification: Verified that globalThis.Headers is correctly populated.

[x] Logic Safety: Added hasOwnProperty check to ensure we only process own properties of the event.headers object.

@usualoma
Copy link
Member

Hi @fcarvajalbrown
Thank you for creating the pull request.

For future pull requests, please include a description following this template:
https://github.com/honojs/hono/blob/main/.github/pull_request_template.md

Regarding this pull request, I believe it is a reasonable small improvement.

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