hostname from url (v21)

Revision 21 of this benchmark created on


Setup

var link = "http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string";
  var host;
  
  function extractDomain(url) {
      var domain;
      //find & remove protocol (http, ftp, etc.) and get domain
      if (url.indexOf("://") > -1) {
          domain = url.split('/')[2];
      }
      else {
          domain = url.split('/')[0];
      }
  
      //find & remove port number
      domain = domain.split(':')[0];
  
      return domain;
  }
  
  function url_domain(data) {
    return (new URL(data)).hostname;
  }
  
  function simple_domain(url)
  {
  var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
  return matches[1];
  }

Test runner

Ready to run.

Testing in
TestOps/sec
extractDomain
host = extractDomain(link);
ready
anchor
host = url_domain(link);
ready
simpledomain
host = simple_domain(link);
ready

Revisions

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