query str parsing: regex vs split

JavaScript performance comparison

Revision 13 of this test case created

Info

http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

Preparation code

<script>
  var query = 'http://your.domain/product.aspx?category=4&product_id=2140&query=lcd+tv';
 
  function split() {
    var queryString = {};
    query.split("?").pop().split("&").forEach(function (prop) {
      var item = prop.split("=");
      queryString[item.shift()] = item.shift();
    });
    return queryString;
  }
 
  function split2() {
    var queryString = {};
    var props = query.substring(1).split("&");
    for (var i = 0; i < args.length; i++) {
      var item = props[i].split("=");
      queryString[item[0]] = item[1];
}
    });
    return queryString;
  }
 
</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
regex
regex()
pending…
split
split()
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