Skip to content

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.

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.48%. Comparing base (df97e5f) to head (40d8dce).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4762   +/-   ##
=======================================
  Coverage   91.48%   91.48%           
=======================================
  Files         177      177           
  Lines       11551    11555    +4     
  Branches     3353     3352    -1     
=======================================
+ Hits        10567    10571    +4     
  Misses        983      983           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

const eventHeaders = event.headers
if (eventHeaders) {
for (const k in eventHeaders) {
if (Object.prototype.hasOwnProperty.call(eventHeaders, k)) {
Copy link
Member

Choose a reason for hiding this comment

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

Checking hasOwnProperty is really necessary? I think the eventHandlers is a plain object ({}), so it's unnecessary. What do you think of it?

Copy link
Contributor

Choose a reason for hiding this comment

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

for...in also enumerates properties in the prototype chain. Object.keys() doesn't, so you can skip hasOwnProperty

Copy link
Member

Choose a reason for hiding this comment

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

@fcarvajalbrown Can you handle this?

Copy link
Author

Choose a reason for hiding this comment

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

yes sorry i will, been busy with work on the weekends but now i am available! cheers!

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.

4 participants