Skip to content

perf: cache parsed req.query per request - #7517

Open
Abdel-Monaam-Aouini wants to merge 2 commits into
expressjs:masterfrom
Abdel-Monaam-Aouini:perf/cache-req-query
Open

Abdel-Monaam-Aouini wants to merge 2 commits into
expressjs:masterfrom
Abdel-Monaam-Aouini:perf/cache-req-query

Conversation

@Abdel-Monaam-Aouini

Copy link
Copy Markdown
Contributor

req.query is a getter that runs the query parser on every access. A handler (or middleware plus handler) that reads it more than once parses the same string each time. For a 5 param query I measured ~1.2us per access with the simple parser and ~3.6us with extended.

This keeps the parsed result on the request and reuses it while req.url and the query parser function are the same. If either changes (url rewrite, setting changed) it parses again, and a parser that throws is not cached.

One behavior change: repeated reads of req.query now return the same object within a request, so a change like req.query.x = 1 is kept for later reads. Before, each read returned a new object and the change was lost. Happy to drop this if you think that is not acceptable.

Added tests for single parse per request, no sharing between requests, url and setting changes, parser errors, disabled parser and mounted sub apps.

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