URL Parsing (v24)

Revision 24 of this benchmark created on


Setup

var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
  function parseWithRE(url){
  	var matches = urlParseRE.exec(url);
      return {
          hash: matches[17],
          search: matches[16],
          pathname: matches[13],
          port: matches[12],
          hostname: matches[11],
          host: matches[10],
          password: matches[9],
          username: matches[8],
          protocol: matches[4],
          origin: matches[4] + "//" + matches[10],
          href: url
      };
  }
  
  var parser = document.createElement('a');
  var url = "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content";
  function _URL(url) {
      // Naive URL parser.  Assumes the input URL is valid.
      this.hash = "";
      this.search = "";
      this.pathname = "";
      this.port = "";
      this.hostname = "";
      this.host = "";
      this.password = "";
      this.username = "";
      this.protocol = "";
      this.origin = "";
      this.href = url;
  
      var hashIdx = url.indexOf("#");
      if (hashIdx > -1) {
          this.hash = url.slice(hashIdx);
          url = url.slice(0, hashIdx);
      }
  
      var searchIdx = url.indexOf("?");
      if (searchIdx > -1) {
          this.search = url.slice(searchIdx);
          url = url.slice(0, searchIdx);
      }
  
      var protoIdx = url.indexOf("://") + 1;
      this.protocol = url.slice(0, protoIdx);
      url = url.slice(protoIdx + 2);
  
      var slashIdx = url.indexOf("/");
      if (slashIdx > -1) {
          this.host = url.slice(0, slashIdx);
          this.pathname = url.slice(slashIdx);
      }
      else {
          this.host = url;
      }
  
      var userIdx = this.host.indexOf("@");
      if (userIdx > -1) {
          this.username = this.host.slice(0, userIdx);
          this.host = this.host.slice(userIdx + 1);
      }
  
      var pwdIdx = this.username.indexOf(":");
      if (pwdIdx > -1) {
          this.password = this.username.slice(pwdIdx + 1);
          this.username = this.username.slice(0, pwdIdx);
      }
  
      var portIdx = this.host.indexOf(":");
      if (portIdx > -1) {
          this.hostname = this.host.slice(0, portIdx);
          this.port = this.host.slice(portIdx + 1);
      }
      else {
          this.hostname = this.host;
      }
  
      this.origin = this.protocol + "//" + this.host;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Regex
var u1 = parseWithRE(url);
var hostname1 = u1.hostname;
var search1 = u1.search;
ready
createElement("a")
parser.href = url;
var hostname2 = parser.hostname;
var search2 = parser.search;
ready
_URL
var u3 = new _URL(url);
var hostname3 = u3.hostname;
var search3 = u3.search;
ready
Native URL
var u4 = new URL(url);
var hostname4 = u4.hostname;
var search4 = u4.search;
ready

Revisions

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