URL Parsing w Lib

JavaScript performance comparison

Revision 5 of this test case created by zaus

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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]
  		};
  	}
  };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Regex
var matches = urlParseRE.exec(url);
var hostname1 = matches[11];
var search1 = matches[16];
pending…
Native
parser.href = url;
var hostname2 = parser.hostname;
var search2 = parser.search;
pending…
Lib With Regex
var parsed3 = UrlParser.get(url);
var hostname3 = parsed3.hostname;
var search3 = parsed3.search;
pending…

Compare results of other browsers

Revisions

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

0 Comments