url parsing

JavaScript performance comparison

Test case created and last updated

Info

Different methods for parsing urls for http://stackoverflow.com/questions/6168260/how-to-parse-a-url

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var anchor = document.createElement('a');
};
</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
single-use anchor
(function(url) {
  var anchor = document.createElement('a');
  anchor.href = url;
  return {
    'protocol': anchor.protocol,
    'hostname': anchor.hostname,
    'path': anchor.pathname,
    'queryString': anchor.search,
    'hash': anchor.hash
  };
})('http://stackoverflow.com/questions/6168260/how-to-parse-a-url?query=string#foo');
pending…
reusable anchor
(function(url) {
  anchor.href = url;
  return {
    'protocol': anchor.protocol,
    'hostname': anchor.hostname,
    'path': anchor.pathname,
    'queryString': anchor.search,
    'hash': anchor.hash
  };
})('http://stackoverflow.com/questions/6168260/how-to-parse-a-url?query=string#foo');
pending…
regex from RFC
(function parse_url(url) {
    var pattern = RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
    var matches =  url.match(pattern);
    return {
        scheme: matches[2],
        authority: matches[4],
        path: matches[5],
        query: matches[7],
        fragment: matches[9]
    };
})('http://stackoverflow.com/questions/6168260/how-to-parse-a-url?query=string#foo')
pending…

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

Compare results of other browsers

0 comments

Add a comment