Skip to content

perf: use byteLength property for binary body chunks#5126

Merged
trivikr merged 1 commit intonodejs:mainfrom
trivikr:body-writer-chunk-size-handling
Apr 30, 2026
Merged

perf: use byteLength property for binary body chunks#5126
trivikr merged 1 commit intonodejs:mainfrom
trivikr:body-writer-chunk-size-handling

Conversation

@trivikr
Copy link
Copy Markdown
Member

@trivikr trivikr commented Apr 27, 2026

This relates to...

Improves HTTP/1 body writer chunk-size handling for binary chunks

Rationale

AsyncWriter.write() used Buffer.byteLength(chunk) for every body chunk.

For Buffer and Uint8Array chunks, chunk.byteLength is cheaper and avoids the string-oriented logic in Buffer.byteLength(). String chunks still need Buffer.byteLength() to preserve byte-accurate content-length and chunked framing behavior.

Changes

  • Use chunk.byteLength when body writer chunks are Uint8Array instances, including Buffer.
  • Keep Buffer.byteLength(chunk) for string chunks and invalid non-binary values.

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

Assisted-by: openai:gpt-5.5

Assisted-by: openai:gpt-5.5
Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
@trivikr
Copy link
Copy Markdown
Member Author

trivikr commented Apr 27, 2026

Benchmarks

benchmark                      avg (min … max) p75 / p99    (min … top 1%)
---------------------------------------------- -------------------------------
• body writer chunk length
---------------------------------------------- -------------------------------
Buffer.byteLength(buffer)         9.21 ns/iter   9.12 ns █                    
                          (9.05 ns … 38.91 ns)  12.80 ns █                    
                       (  0.10  b …  57.16  b)   0.15  b █▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

buffer.byteLength               362.41 ps/iter 356.20 ps           █          
                        (325.44 ps … 14.06 ns) 386.47 ps           █          
                       (  0.09  b …  32.11  b)   0.09  b ▁▁▁▁▁▁▁▅▁▁█▁▁▂▁▁▁▁▁▁▁

Buffer.byteLength(uint8Array)     9.60 ns/iter   9.35 ns █                    
                          (9.30 ns … 47.80 ns)  14.59 ns █                    
                       (  0.10  b …  56.11  b)   0.14  b █▃▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

uint8Array.byteLength           396.28 ps/iter 396.73 ps             █        
                        (366.21 ps … 14.03 ns) 416.99 ps         █   █        
                       (  0.09  b …  84.19  b)   0.09  b ▁▁▁▁▁▁▁▁█▁▁▁█▁▁▁▂▁▁▁▁

Buffer.byteLength(string)         2.54 µs/iter   2.54 µs █ ▅                  
                           (2.52 µs … 2.60 µs)   2.59 µs █▇█                  
                       (  0.10  b …   1.12  b)   0.13  b ███▇▄▇▄▇▄▆▄▄▁▁▁▁▁▁▁▁▃

• body writer chunk length helper
---------------------------------------------- -------------------------------
previousChunkLength(buffer)      12.99 ns/iter  12.93 ns  █                   
                         (12.87 ns … 36.76 ns)  14.23 ns ▄█                   
                       (  0.09  b …  44.11  b)   0.17  b ██▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁

currentChunkLength(buffer)      399.13 ps/iter 396.73 ps             █        
                        (366.21 ps … 14.01 ns) 417.24 ps             █        
                       (  0.09  b …  44.73  b)   0.09  b ▁▁▁▁▁▁▁▁▆▁▁▁█▁▁▁▂▁▁▁▁

previousChunkLength(uint8Array)   9.30 ns/iter   9.26 ns █                    
                          (9.22 ns … 24.05 ns)  10.66 ns ██                   
                       (  0.10  b …  56.10  b)   0.15  b ██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

currentChunkLength(uint8Array)    1.96 ns/iter   1.94 ns    █                 
                          (1.92 ns … 15.85 ns)   2.04 ns    █                 
                       (  0.09  b …  52.10  b)   0.11  b ▁▁▃█▁▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

previousChunkLength(string)       2.55 µs/iter   2.54 µs █▂                   
                           (2.52 µs … 2.70 µs)   2.69 µs ██                   
                       (  0.10  b …   2.01  b)   0.14  b ██▇▅▁▂▁▂▁▁▂▃▁▅▁▁▁▁▁▁▂

currentChunkLength(string)        2.53 µs/iter   2.54 µs █  ▂                 
                           (2.53 µs … 2.60 µs)   2.56 µs █▇▃█                 
                       (  0.10  b …   0.74  b)   0.12  b ████▁▂▁▂▅▄▁▄▄▂▁▁▁▂▂▅▂

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.14%. Comparing base (9a6e313) to head (24e4790).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5126   +/-   ##
=======================================
  Coverage   93.14%   93.14%           
=======================================
  Files         110      110           
  Lines       36117    36117           
=======================================
  Hits        33642    33642           
  Misses       2475     2475           

☔ 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.

@trivikr trivikr merged commit f41e53f into nodejs:main Apr 30, 2026
35 checks passed
@trivikr trivikr deleted the body-writer-chunk-size-handling branch April 30, 2026 13:35
@github-actions github-actions Bot mentioned this pull request May 1, 2026
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.

3 participants