hostname without www

Benchmark created on


Description

What is faster? Using regex or build a html a element.

Test runner

Ready to run.

Testing in
TestOps/sec
regex
var urls = ['http://www.google.com', 'https://www.google.fr', 'https://mail.google.com', 'test.google.com', 'www.google.com'];

var getHostname = function(url) {
  var match = url.match(/(?:https?:\/\/)?(?:www\.)?(.*?)\//);
  return match[match.length - 1];
};

for (var i = 0; i < urls.length; i++) {
  var hostname = getHostname(urls[i]);
}
ready
a element
var urls = ['http://www.google.com', 'https://www.google.fr', 'https://mail.google.com', 'test.google.com', 'www.google.com'];

var getHostname = function(url) {
  var a = document.createElement('a');
  a.href = url;
  var domain = a.hostname.replace(/^www\./, "");
  delete a;
  return domain
};

for (var i = 0; i < urls.length; i++) {
  var hostname = getHostname(urls[i]);
}
ready

Revisions

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