Skip to content

perf: set default html and json content types directly - #7516

Open
Abdel-Monaam-Aouini wants to merge 1 commit into
expressjs:masterfrom
Abdel-Monaam-Aouini:perf/default-content-types
Open

Abdel-Monaam-Aouini wants to merge 1 commit into
expressjs:masterfrom
Abdel-Monaam-Aouini:perf/default-content-types

Conversation

@Abdel-Monaam-Aouini

Copy link
Copy Markdown
Contributor

res.send(string) calls res.type('html') and res.json() sets application/json when no Content-Type is set. Both go through a mime lookup (mime.contentType) just to get back text/html; charset=utf-8 and application/json; charset=utf-8.

This sets those two values directly, so the header is the same but the lookup is skipped. A micro benchmark shows the lookup costs ~200ns for html and ~100ns for application/json. I did not see a difference in a full request benchmark, it is within noise, so this is a small saving per response.

Added tests that check the exact header value, that there is a single Content-Type header, HEAD requests, and that a type set earlier with res.type() is kept.

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.

1 participant