close
Skip to content

feat: do not modify the Content-Type twice when sending strings#6991

Merged
bjohansebas merged 6 commits into
masterfrom
bsebas/no-set-twice-content-type
Jan 19, 2026
Merged

feat: do not modify the Content-Type twice when sending strings#6991
bjohansebas merged 6 commits into
masterfrom
bsebas/no-set-twice-content-type

Conversation

@bjohansebas

@bjohansebas bjohansebas commented Jan 16, 2026

Copy link
Copy Markdown
Member

We were modifying the Content-Type twice when a manual Content-Type was not set (that is, without res.set('Content-Type', 'text/plain').send('hey')), because it would enter here and then enter here again. This reduces how often we go into the content-type library, adds a small performance improvement, and slightly improves CPU usage, building on all the validations that

exports.setCharset = function setCharset(type, charset) {
already did.

For those who do comparisons with a hello-world, this will improve Express’s results, we’re almost catching up to Fastify 😄

The following diffs are based on the simple hello-world example in this repo (https://github.com/expressjs/express/tree/master/examples/hello-world)

diff v4.22.1 vs this pr

image

diff v4.22.1 vs v5.2.1

image

@bjohansebas bjohansebas added 5.x semver-patch This change is a semver patch performance ⚡ labels Jan 16, 2026
@bjohansebas bjohansebas requested review from a team and wesleytodd January 16, 2026 02:47

@ShubhamOulkar ShubhamOulkar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Methodology

  • Scenario: res.send('<html>...</html>')
  • Iterations: 100,000
  • Environment: Windows / Node.js
  • Metric: Operations per second (ops/sec)

Results

Metric Before (Current) After (Optimized) Improvement
Total Time ~501 ms ~137 ms ~3.6x Faster
Avg Op Time 0.0050 ms 0.0014 ms -72% overhead
Throughput ~200k ops/sec ~728k ops/sec +264%

Comment thread lib/response.js

@jonchurch jonchurch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, just some nits

Comment thread lib/response.js Outdated
Comment thread lib/response.js Outdated
Comment thread lib/response.js Outdated
Comment thread lib/response.js Outdated
@bjohansebas bjohansebas requested a review from jonchurch January 16, 2026 21:16
Comment thread lib/response.js

@Phillip9587 Phillip9587 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@bjohansebas bjohansebas merged commit a479419 into master Jan 19, 2026
28 checks passed
@bjohansebas bjohansebas deleted the bsebas/no-set-twice-content-type branch January 19, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.x performance ⚡ semver-patch This change is a semver patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants