cloudflare / cloudflare/workerd
🐛 Bug Report — Runtime APIs: URL.searchParams performance issue
- Dominant language
- C++
- Stars
- 8.7k
- Forks
- 739
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 174
Description
👋 It seems that workerd suffers from the same performance issue that I fixed in Node.js (https://github.com/nodejs/node/pull/51520), where repeat writes to `URL.searchParams` trigger `URL` to re-parse the params on every write, leading to a performance bottleneck.
This can be seen in the [Workers playground](https://workers.cloudflare.com/playground):
```ts
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === '/url') {
const params = new URL(request.url).searchParams;
for (let i = 0; i < 100_000; i++) params.append('test', i.toString());
}
if (url.pathname === '/urlsearchparams') {
const params = new URLSearchParams();
for (let i = 0; i < 100_000; i++) params.append('test', i.toString());
}
return Response.json({ ok: true });
},
};
```
A request to `/url` will time out, but a request to `/urlsearchparams` runs without issue.
I suspect a patch similar to what I landed in Node.js, where URL is lazily updated if searchParams has changed the next time a getter is called, rather than immediately updating URL when searchParams changes, would fix it.
Alas, I'm not confident enough w/ C++ and this codebase to submit a patch myself I suspect -- perhaps if someone can point me in the direction of where URLSearchParams talks back to URL, I might be able to figure it out from there?
Contributor guide
Assessment
This issue has not been assessed yet.