query str parsing: regex vs split

JavaScript performance comparison

Test case created by Josh

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 regex() {
    var queryString = {};
    query.replace(/([^?=&]+)(=([^&]*))?/g, function ($0, $1, $2, $3) {
      queryString[$1] = $3;
    });
  
    return queryString;
  }
  
</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
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.

0 Comments