perf: reduce EventSourceStream parser allocations - #5032
Merged
Conversation
Assisted-by: OpenAI:gpt-5.4 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Member
Author
BenchmarksBefore$ node benchmarks/eventsource/eventsource-stream.mjs
clk: ~3.18 GHz
cpu: Apple M1 Pro
runtime: node 24.14.0 (arm64-darwin)
benchmark avg (min âĶ max) p75 / p99 (min âĶ top 1%)
------------------------------------------- -------------------------------
âĒ EventSourceStream parsing (250 events)
------------------------------------------- -------------------------------
single chunk 523.13 Âĩs/iter 517.21 Âĩs â
(493.29 Âĩs âĶ 853.88 Âĩs) 731.96 Âĩs â
(167.25 kb âĶ 1.18 mb) 659.19 kb âââââââââââââââââââââ
256-byte chunks 544.53 Âĩs/iter 538.29 Âĩs â
(519.42 Âĩs âĶ 1.71 ms) 749.08 Âĩs ââ
(110.61 kb âĶ 2.11 mb) 711.91 kb âââââââââââââââââââââ
64-byte chunks 597.89 Âĩs/iter 600.50 Âĩs â
(580.54 Âĩs âĶ 844.83 Âĩs) 726.08 Âĩs ââ
(148.83 kb âĶ 1.41 mb) 868.50 kb âââââââââââââââââââââ
8-byte chunks 1.14 ms/iter 1.14 ms â â
(1.11 ms âĶ 1.30 ms) 1.27 ms ââââ
(659.22 kb âĶ 2.29 mb) 2.29 mb âââââââââââââââââââââ
1-byte chunks 5.49 ms/iter 5.56 ms ââââ
(5.20 ms âĶ 6.90 ms) 6.63 ms âââââââ
( 13.86 mb âĶ 13.86 mb) 13.86 mb âââââââââââââââââââââ
âĒ EventSourceStream parsing with BOM
------------------------------------------- -------------------------------
BOM + 8-byte chunks 1.16 ms/iter 1.17 ms â
(1.13 ms âĶ 1.39 ms) 1.34 ms â â
( 2.29 mb âĶ 2.30 mb) 2.29 mb âââââ
â
âââââââââââââââ
BOM + 1-byte chunks 5.42 ms/iter 5.49 ms ââ
(5.32 ms âĶ 5.74 ms) 5.70 ms ââ
( 13.86 mb âĶ 13.87 mb) 13.86 mb ââââââââââââââ
âââââââAfter$ node benchmarks/eventsource/eventsource-stream.mjs
clk: ~3.14 GHz
cpu: Apple M1 Pro
runtime: node 24.14.0 (arm64-darwin)
benchmark avg (min âĶ max) p75 / p99 (min âĶ top 1%)
------------------------------------------- -------------------------------
âĒ EventSourceStream parsing (250 events)
------------------------------------------- -------------------------------
single chunk 356.42 Âĩs/iter 333.46 Âĩs ââ
(320.71 Âĩs âĶ 6.80 ms) 661.83 Âĩs ââ
( 10.12 kb âĶ 1.12 mb) 210.89 kb âââââââââââââââââââââ
256-byte chunks 364.56 Âĩs/iter 363.38 Âĩs â
(352.96 Âĩs âĶ 517.63 Âĩs) 443.25 Âĩs âââ
( 62.13 kb âĶ 1.30 mb) 301.20 kb âââââââââââââââââââââ
64-byte chunks 465.56 Âĩs/iter 463.33 Âĩs ââ
(445.42 Âĩs âĶ 650.08 Âĩs) 588.88 Âĩs â
ââ
( 14.05 kb âĶ 1.09 mb) 552.85 kb âââââââââââââââââââââ
8-byte chunks 1.01 ms/iter 1.00 ms â
(976.13 Âĩs âĶ 1.22 ms) 1.13 ms âââ
( 1.08 mb âĶ 2.11 mb) 1.83 mb âââââââââââââââââââââ
1-byte chunks 5.10 ms/iter 5.10 ms â
(4.84 ms âĶ 6.46 ms) 6.43 ms â â
( 11.93 mb âĶ 11.93 mb) 11.93 mb âââââââââââââââââââââ
âĒ EventSourceStream parsing with BOM
------------------------------------------- -------------------------------
BOM + 8-byte chunks 1.02 ms/iter 1.02 ms â
(993.00 Âĩs âĶ 1.33 ms) 1.19 ms â
( 1.84 mb âĶ 1.88 mb) 1.84 mb âââââââââââââââââââââ
BOM + 1-byte chunks 5.13 ms/iter 5.20 ms ââ
(4.96 ms âĶ 5.37 ms) 5.36 ms âââ â â
( 11.93 mb âĶ 11.93 mb) 11.93 mb â
âââ
âââââââââââââ
â
â
â
â |
Codecov Reportâ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5032 +/- ##
==========================================
+ Coverage 93.14% 93.16% +0.01%
==========================================
Files 110 110
Lines 36117 36212 +95
==========================================
+ Hits 33642 33736 +94
- Misses 2475 2476 +1 â View full report in Codecov by Sentry. ð New features to boost your workflow:
|
KhafraDev
approved these changes
Apr 30, 2026
KhafraDev
left a comment
Member
There was a problem hiding this comment.
EventSource isn't really my area of expertise.
Merged
mcollina
added a commit
that referenced
this pull request
Aug 4, 2026
mcollina
added a commit
that referenced
this pull request
Aug 4, 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.
This relates to...
Fixes: #2630
Rationale
EventSourceStreamwas doing avoidable work on long-lived SSE streams by concatenating the buffered input for every chunk, allocating strings for every parsed field name, and replacing the internal event object after each dispatch. That creates unnecessary allocation churn in the hot path, especially when servers deliver heavily fragmented chunks.Changes
Buffer.concat([buffer, chunk])approach with chunk-list buffering plus cursors.data,event,id, andretryfield names from bytes instead of decoding field strings first.retryandiddirectly from bytes before decoding their values.EventSourceStreambenchmarks covering realistic payloads, fragmented chunking, and BOM-prefixed input.Features
N/A
Bug Fixes
Reduced allocation pressure in the
EventSourceStreamparser hot path for fragmented SSE streams.Breaking Changes and Deprecations
processEvent(event)now receives a reused parser event object instead of a freshly allocated object each time.Status