perf(aws-lambda): optimize header parsing to reduce micro-allocations#4762
perf(aws-lambda): optimize header parsing to reduce micro-allocations#4762fcarvajalbrown wants to merge 1 commit intohonojs:mainfrom
Conversation
|
Hi @fcarvajalbrown For future pull requests, please include a description following this template: Regarding this pull request, I believe it is a reasonable small improvement. |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| const eventHeaders = event.headers | ||
| if (eventHeaders) { | ||
| for (const k in eventHeaders) { | ||
| if (Object.prototype.hasOwnProperty.call(eventHeaders, k)) { |
There was a problem hiding this comment.
Checking hasOwnProperty is really necessary? I think the eventHandlers is a plain object ({}), so it's unnecessary. What do you think of it?
There was a problem hiding this comment.
for...in also enumerates properties in the prototype chain. Object.keys() doesn't, so you can skip hasOwnProperty
There was a problem hiding this comment.
yes sorry i will, been busy with work on the weekends but now i am available! cheers!
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