kit icon indicating copy to clipboard operation
kit copied to clipboard

`cookies.get()` re-parses the cookie header on every call

Open Nic-Polumeyv opened this issue 4 months ago • 0 comments

Describe the problem

I was in cookie.js recently for #16322 and noticed that get_cookies parses the header once into initial_cookies, but with an identity decode, so that result only serves get_cookie_header. Every cookies.get() call runs a fresh parse() of the entire header, and also builds, filters and sorts an array of locally set cookies even when there are none, which is the common case. handle, server loads and actions all call cookies.get, so a typical request pays this several times.

A parse costs about 1µs per KB of header, and today's code pays it on every get instead of once per request, so a request making five gets against a 4KB header wastes ~16µs. Small, but it is pure waste on the hot path of every request.

Describe the proposed solution

Lazily cache the default-decode parse, the header is immutable for the lifetime of the request, and a custom opts.decode bypasses the cache. Skip the new_cookies scan when the map is empty. A few lines, no behavior change. I'll open a PR.

Importance

nice to have

Nic-Polumeyv avatar Jul 14 '26 03:07 Nic-Polumeyv