workerd icon indicating copy to clipboard operation
workerd copied to clipboard

🐛 Bug Report — Runtime APIs: URL.searchParams performance issue

Open MattIPv4 opened this issue 1 year ago • 0 comments

👋 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:

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?

MattIPv4 avatar Mar 26 '24 20:03 MattIPv4