Finding components of a URL

JavaScript performance comparison

Revision 4 of this test case created by Steven Levithan

Info

I want to know which method of detecting the components of a URL works best.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var url = "/providerID/specificJob/debug/count";
};
</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
String indexOf
var providerID = url.indexOf('providerID') > -1;
var specificJob = url.indexOf('specificJob') > -1;
var debug = url.indexOf('debug') > -1;
var count = url.indexOf('count') > -1;
pending…
Regex Search
// Not a fair test, since in practice you would create the regexes
// once only, rather than each time though the loop

var providerID = url.search(/providerID/) > -1;
var specificJob = url.search(/specificJob/) > -1;
var debug = url.search(/debug/) > -1;
var count = url.search(/count/) > -1;
pending…
Regex Test
// Not a fair test, since in practice you would create the regexes
// once only, rather than each time though the loop

var providerID = /providerID/.test(url);
var specificJob = /specificJob/.test(url);
var debug = /debug/.test(url);
var count = /count/.test(url);
pending…
Regex Match
// Not a fair test, since in practice you would create the regexes
// once only, rather than each time though the loop

var providerID = !!url.match(/providerID/);
var specificJob = !!url.match(/specificJob/);
var debug = !!url.match(/debug/);
var count = !!url.match(/count/);
pending…
Regex Match with type conversion from string
var providerID = !!url.match('providerID');
var specificJob = !!url.match('specificJob');
var debug = !!url.match('debug');
var count = !!url.match('count');
pending…
Split then check if it's equal
var urlArray = url.split('/');
var n = urlArray.length;

var providerID = false;
var specificJob = false;
var debug = false;
var count = false;

while (n--) {
  if (urlArray[n] === 'providerID') providerID = true;
  if (urlArray[n] === 'specificJob') specificJob = true;
  if (urlArray[n] === 'debug') debug = true;
  if (urlArray[n] === 'count') count = true;
}
pending…
Split and then indexOf
var urlArray = url.split('/');

var providerID = urlArray.indexOf('providerID');
var specificJob = urlArray.indexOf('specificJob');
var debug = urlArray.indexOf('debug');
var count = urlArray.indexOf('count');
pending…
Single Regex
// Not a fair test, since in practice you would create the regexes
// once only, rather than each time though the loop

var regex = /(providerID)|(specificJob)|(debug)|(count)/g;
var n = 4;
var match;

var providerID = false;
var specificJob = false;
var debug = false;
var count = false;

while (match = regex.exec(url)) {
  if (match[1]) providerID = true;
  if (match[2]) specificJob = true;
  if (match[3]) debug = true;
  if (match[4]) count = true;
}
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