URL Parsing w Lib (v5)

Revision 5 of this benchmark created by zaus on


Setup

var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
  var parser = document.createElement('a');
  var url = "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content";
  
  
  var UrlParser = {
  	regx: /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/
  	,
  	get : function(url) {
  		var matches = this.regx.exec(url);
  		// map results
  		return {
  			href: matches[0],
  			withoutHash: matches[1],
  			url: matches[2],
  			origin: matches[3],
  			protocol: matches[4],
  			protocolseparator: matches[5],
  			credhost: matches[6],
  			cred: matches[7],
  			user: matches[8],
  			pass: matches[9],
  			host: matches[10],
  			hostname: matches[11],
  			port: matches[12],
  			pathname: matches[13],
  			segment1: matches[14],
  			segment2: matches[15],
  			search: matches[16],
  			hash: matches[17]
  		};
  	}
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Regex
var matches = urlParseRE.exec(url);
var hostname1 = matches[11];
var search1 = matches[16];
ready
Native
parser.href = url;
var hostname2 = parser.hostname;
var search2 = parser.search;
ready
Lib With Regex
var parsed3 = UrlParser.get(url);
var hostname3 = parsed3.hostname;
var search3 = parsed3.search;
ready

Revisions

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