Performance test from jsPerf indicates using regular expression will be faster.
I used the regular expression from javascript the good parts. This is a textbook example and it runs 73% faster than your code so expect production quality code to do better and equal in the worst case.
URL to benchmark http://jsperf.com/parse-url.
And the code is used is:
function parse_url_regex(url) {
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var result = parse_url.exec(url);
return result;
}
But I highly suggest you try codereview.stackexchange.com