Finding components of a URL

JavaScript performance comparison

Revision 6 of this test case created

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";
   
    /*
     *NOTE: In revision 1 I removed the last test, for some reason my chrome and firefox froze although I think the test was fine... */

};
</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
indexOf
var providerID = url.indexOf('providerID');
var specificJob = url.indexOf('specificJob');
var debug = url.indexOf('debug');
var count = url.indexOf('count');
pending…
Regex Match
var providerID = url.match('providerID').length;
var specificJob = url.match('specificJob').length;
var debug = url.match('debug').length;
var count = url.match('count').length;
pending…
Regex Search
var providerID = url.search('providerID');
var specificJob = url.search('specificJob');
var debug = url.search('debug');
var count = url.search('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…
Split then check if it's equal 2
var urlArray = url.split('/');
var n = urlArray.length;

var o = {
  'providerID': false,
  'specificJob ': false,
  'debug ': false,
  'count': false
};
var a = Object.keys(o),
  l = a.length,
  i = 0;

while (n--) {
  for (i = 0; i < l; i++)
    if (urlArray[n] === a[i]) o[a[i]] = 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