URL: canUse vs try..catch

Benchmark created on


Setup

const fn1 = (url) => {
	if (URL.canParse(url)) {
		return new URL(url);
	}
	return null;
};


const fn2 = (url) => {
	try {
		return new URL(url);
	} catch {
		return null;
	}
};


const URLs = [
	'https://example.com',
	'10.0.0.1',
	'example.com',
	'ftp://example.com/abc',
	'http://www.example.com',
	'chrome://flags',
	'http://128.0.0.1:8080',
];

Test runner

Ready to run.

Testing in
TestOps/sec
canParse
URLs.forEach(fn1)
ready
try..catch
URLs.forEach(fn2)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.