query str parsing: regex vs split (v2)

Revision 2 of this benchmark created on


Description

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

Preparation HTML

<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[0]] = item[1];
    });
    return queryString;
  }
  
  function splitFor() {
    var queryString = {},
        parts = query.split("?").pop().split("&"),
        i = 0, l = parts.length, item;
  
    for (; i < l; i++) {
      item = parts[i].split("=");
      queryString[item[0]] = item[1];
    }
    return queryString;
  }
  
  function regex() {
    var queryString = {};
    query.replace(/([^?=&]+)(=([^&]*))?/g, function ($0, $1, $2, $3) {
      queryString[$1] = $3;
    });
  
    return queryString;
  }
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
regex
regex()
ready
split
split()
ready
split with for loop
splitFor()
ready

Revisions

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