URL Parsing (v26)

Revision 26 of this benchmark created by Zsolt M on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.11.2/URI.min.js"></script>

Setup

var parser = document.createElement('a');
  var url = "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content";
  
  var parseURL = (function() {
    var PARSE_RE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
  
  
    return function(url) {
      var matches = PARSE_RE.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 = parseURL(url);
var hostname1 = matches[11];
var search1 = matches[16];
ready
Native
parser.href = url;
var hostname2 = parser.hostname;
var search2 = parser.search;
ready
URI.js
var uri = URI(url);
var hostname3 = uri.hostname();
var search3 = uri.search();
ready
built in URL() class
var uri = new URL(url);
var hostname3 = uri.hostname;
var search3 = uri.search;
ready

Revisions

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