Get hostname from URL() or from regexp

Benchmark created on


Setup

const url = document.URL;

Test runner

Ready to run.

Testing in
TestOps/sec
New URL
const Url = new URL(url);
const host = URL.hostname
ready
Regexp
const regexp = /^(?<protocol>https?|file):\/\/(?<host>[^:\/\s]+)?:?(?<port>\d{1,5})?\/?(?<path>[^#?\s]*)?\??(?<query>[^#\s]*)?(?<hash>#.*)?$/gim;
const Url = regexp.exec(url);
const host = Url.groups.host;
ready

Revisions

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