hostname from url (v29)

Revision 29 of this benchmark created on


Setup

var link = "https://www.vn.search.yahoo.com.vn";
  var host;
  
  // parseUri 1.2.2
  // (c) Steven Levithan <stevenlevithan.com>
  // MIT License
  
  function parseUri (str) {
  	var	o   = parseUri.options,
  		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
  		uri = {},
  		i   = 14;
  
  	while (i--) uri[o.key[i]] = m[i] || "";
  
  	uri[o.q.name] = {};
  	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  		if ($1) uri[o.q.name][$1] = $2;
  	});
  
  	return uri;
  };
  
  parseUri.options = {
  	strictMode: false,
  	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  	q:   {
  		name:   "queryKey",
  		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  	},
  	parser: {
  		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  	}
  };
    var    a      = document.createElement('a');
  function url_domain(data) {
  
           a.href = data;
    return a.hostname;
  }
  
  function simple_domain(url)
  {
  var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
  return matches[1];
  }
  
  function getDomainName(domain) {
          var parts = domain.split('.').reverse();
          var cnt = parts.length;
          if (cnt >= 3) {
              // see if the second level domain is a common SLD.
              if (parts[1].match(/^(com|edu|gov|net|mil|org|nom|co|name|info|biz)$/i)) {
                 return parts[2] + '.' + parts[1] + '.' + parts[0];
              }
          }
          return parts[1]+'.'+parts[0];
      }

Test runner

Ready to run.

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

Revisions

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