Lazy URL Parsing

JavaScript performance comparison

Revision 4 of this test case created by Josh

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    urls = [
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash",
    "http://example.com:3000/pathname/?search=test#hash"
    ];
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
With Cached DOM element
var parser = document.createElement('a');

for(var i = 0, l = urls.length; i<l; i++){

parser.href = urls[i];
console.log(parser.protocol, parser.hostname, parser.port, parser.pathname, parser.search, parser.hash, parser.host)
}
pending…
With Regex
// 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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
        }
};

for(var i = 0, l = urls.length; i<l; i++){
console.log(parseUri(urls[i]));
}
pending…
With uncached DOM element
for(var i = 0, l = urls.length; i<l; i++){
var parser = document.createElement('a');

parser.href = urls[i];
console.log(parser.protocol, parser.hostname, parser.port, parser.pathname, parser.search, parser.hash, parser.host)
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment