Skip to content

Conversation

@bjohansebas
Copy link
Member

@bjohansebas bjohansebas commented Jan 16, 2026

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
Copy link
Member

@ShubhamOulkar ShubhamOulkar left a comment

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%

if (typeof type === "string") {
this.set('Content-Type', setCharset(type, 'utf-8'));
} else {
this.type('html');
Copy link
Member

Choose a reason for hiding this comment

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

We are no longer setting charset now when using the fallback html content type

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.

3 participants