Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to decode URI path segments #23880

Closed
szmarczak opened this issue May 18, 2024 · 0 comments
Closed

API to decode URI path segments #23880

szmarczak opened this issue May 18, 2024 · 0 comments

Comments

@szmarczak
Copy link

szmarczak commented May 18, 2024

Currently, using decodeURI has a big performance impact when decoding segments.

Edit: I moved the percent > query condition to the top and it seems to trigger an optimization. This performs great now.

const path = '/?foo=bar';
const worstPath = '%25/%25/%25/%25/%25/%25/%25/%25';

const decodeURISafe = (uri: string): string => {
	try {
		const decoded = decodeURI(uri);

		// Prevent double-decoding
		if (uri.includes('%25')) {
			return decoded.replaceAll('%', '%25');
		}

		return decoded;
	} catch {
		return uri;
	}
};

const decode = (path: string): [string[], string, number] => {
	const start = path.charCodeAt(0) === /* / */ 47 ? 1 : 0;
	const percent = path.indexOf('%', start);
	const query = path.indexOf('?', start);

	if (percent === -1 || percent > query) {
		if (query === -1) {
			return [
				path.split('/'),
				'',
				start,
			];
		}

		return [
			path.slice(0, query).split('/'),
			path.slice(query),
			start,
		];
	}

	if (query === -1) {
		return [
			decodeURISafe(path).split('/'),
			'',
			start,
		];
	}

	if (percent < query) {
		return [
			decodeURISafe(path.slice(0, query)).split('/'),
			path.slice(query),
			start,
		];
	}

	throw 'unreachable';
};

console.log(decode(worstPath));

Deno.bench('current', () => {
	decode(path);
});

Deno.bench('current - worst case', () => {
	decode(worstPath);
});

Deno.bench('theoretical max', () => {
	path.split('/');
});

Deno.bench('theoretical max - worst case', () => {
	worstPath.split('/');
});
benchmark                         time (avg)        iter/s             (min … max)       p75       p99      p995
---------------------------------------------------------------------------------- -----------------------------
current                           58.01 ns/iter  17,238,049.6   (55.98 ns … 87.85 ns) 58.1 ns 73.1 ns 80.14 ns
current - worst case               48.4 ns/iter  20,663,286.6   (45.13 ns … 57.71 ns) 49.66 ns 53.19 ns 54.35 ns
theoretical max                   31.49 ns/iter  31,759,892.1   (30.97 ns … 39.53 ns) 31.13 ns 35.95 ns 36.22 ns
theoretical max - worst case      32.03 ns/iter  31,218,380.2   (31.28 ns … 43.82 ns) 31.63 ns 36.52 ns 37.35 ns

Proposal

Deno.decodePath('') = [];

Deno.decodePath('/') = [];

Deno.decodePath('/foo') = [
    'foo',
];

Deno.decodePath('foo') = [
    'foo',
];

Deno.decodePath('/foo/%2F') = [
    'foo',
    '/',
];

Deno.decodePath('/foo/%2525') = [
    'foo',
    '%25',
];

Deno.decodePath('/foo/%C4%85%C4%99') = [
    'foo',
    'ąę',
];

Deno.decodePath('/foo/%99') = null;

Feel free to close if that's something out of Deno scope.

@szmarczak szmarczak closed this as not planned Won't fix, can't repro, duplicate, stale May 21, 2024
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

No branches or pull requests

1 participant