url parameter performance

JavaScript performance comparison

Test case created

Preparation code

Regex Results:
<div id='regexResults'>
</div>
<br>
<br>
forEach Results:
<div id='foreachResults'>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var startURL = "?var1=val1&var2=val2&complicated=aVeryLongString";
};

Benchmark.prototype.teardown = function() {
    ui.benchmarks[0].teardown = function() {
      $("#regexResults").text(JSON.stringify(params));
    };
    ui.benchmarks[1].teardown = function() {
      $("#foreachResults").text(JSON.stringify(params));
    };
};
</script>

Preparation code output

Regex Results:


forEach Results:

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
var url = startURL.split("+").join(" ");
var params = {}, tokens, re = /[?&]?([^=]+)=([^&]*)/g;

while (tokens = re.exec(url)) {
  params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
pending…
forEach
var params = {};
startURL.substr(1).split('&').forEach(function(p) {
  p = p.split('=');
  params[p[0]] = decodeURIComponent(p[1] || 1);
});
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