Finding components of a URL

JavaScript performance comparison

Test case created by João Jerónimo


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

Preparation code

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... */


Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
var providerID = url.indexOf('providerID');
var specificJob = url.indexOf('specificJob');
var debug = url.indexOf('debug');
var count = url.indexOf('count');
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;
Regex Search
var providerID ='providerID');
var specificJob ='specificJob');
var debug ='debug');
var count ='count');
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;
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');

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment