perf(context): add fast path to c.json() matching c.text() optimization#4707
Merged
yusukebe merged 5 commits intohonojs:nextfrom Feb 19, 2026
Merged
perf(context): add fast path to c.json() matching c.text() optimization#4707yusukebe merged 5 commits intohonojs:nextfrom
yusukebe merged 5 commits intohonojs:nextfrom
Conversation
Skip #newResponse() and Headers allocation when no status, headers, or finalized state exists. Creates Response directly with inline Content-Type header, matching the existing c.text() fast path pattern.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #4707 +/- ##
==========================================
+ Coverage 91.43% 91.44% +0.01%
==========================================
Files 173 173
Lines 11382 11424 +42
Branches 3302 3319 +17
==========================================
+ Hits 10407 10447 +40
- Misses 974 976 +2
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
yusukebe
reviewed
Feb 16, 2026
Member
|
This PR is super cool. Sorry! I completely misunderstood. The file size is slightly increased. But it's interesting that we can use I would like to merge this. Can you handle my comments? Or, I'll fix them myself. |
yusukebe
approved these changes
Feb 19, 2026
yusukebe
added a commit
that referenced
this pull request
Feb 23, 2026
4 tasks
yusukebe
added a commit
that referenced
this pull request
Feb 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
c.text()already has a fast path (added in an earlier optimization) that returnsnew Response(text)directly when no status, headers, or finalized state exists.c.json()was missing this optimization and always went through#newResponse(), which unconditionally allocates aHeadersobject and iterates header entries — even for the simplestc.json({ ... })call.This PR adds the same 5-condition fast path to
c.json().What changed
In
src/context.ts, thejson()method now checks:When all conditions are met (the common case for simple JSON responses), it creates the Response directly with an inline header object instead of going through
#newResponse():Why this matters
In the
@hono/node-serveradapter, this has a compounding effect. The lightweightResponseconstructor stores inline header objects directly in its cache. When the adapter writes the response, it checksheader instanceof Headers— a plain object skips thebuildOutgoingHttpHeaders()conversion entirely, while aHeadersinstance requires iteration and conversion toOutgoingHttpHeaders.Benchmark results (nodejs)
Body benchmark improvement is primarily from
@hono/node-serverdirect body reading; this PR contributes to the overall gains by eliminating allocations on the response path.Benchmarked with bun-http-framework-benchmark using
bombardierat 500 concurrent connections for 10s per test on Node.js.Thanks for the great lib :)