minor speed improvements or URL parsing

This commit is contained in:
Misko Hevery 2010-07-30 11:45:52 -07:00
parent cdda664f89
commit 1e1c8c82f9

View file

@ -24,7 +24,7 @@ angularService("$location", function(browser){
if (href) { if (href) {
parseUrl(href); parseUrl(href);
} else { } else {
href = check('href') || check('protocol', '://', 'host', ':', 'port', '', 'path', '?', 'search'); href = check('href') || checkProtocol();
var hash = check('hash'); var hash = check('hash');
if (isUndefined(hash)) hash = checkHashPathSearch(); if (isUndefined(hash)) hash = checkHashPathSearch();
if (isDefined(hash)) { if (isDefined(hash)) {
@ -38,19 +38,22 @@ angularService("$location", function(browser){
} }
} }
function check() { function check(param) {
var i = -1, return lastLocation[param] == location[param] ? undefined : location[param];
length=arguments.length, }
name, seperator, parts = [],
value, same = true; function checkProtocol(){
for(; i<length; i = i+2) { if (lastLocation.protocol === location.protocol &&
parts.push(seperator = (arguments[i] || '')); lastLocation.host === location.host &&
name = arguments[i + 1]; lastLocation.port === location.port &&
value=location[name]; lastLocation.path === location.path &&
parts.push(typeof value == 'object' ? toKeyValue(value) : value); equals(lastLocation.search, location.search))
same = same && equals(lastLocation[name], value); return undefined;
} var url = toKeyValue(location.search);
return same ? undefined : parts.join(''); var port = (location.port == DEFAULT_PORTS[location.protocol] ? null : location.port);
return location.protocol + '://' + location.host +
(port ? ':' + port : '') + location.path +
(url ? '?' + url : '');
} }
function checkHashPathSearch(){ function checkHashPathSearch(){
@ -71,9 +74,7 @@ angularService("$location", function(browser){
location.port = match[5] || DEFAULT_PORTS[location.protocol] || null; location.port = match[5] || DEFAULT_PORTS[location.protocol] || null;
location.path = match[6]; location.path = match[6];
location.search = parseKeyValue(match[8]); location.search = parseKeyValue(match[8]);
location.hash = match[9] || ''; location.hash = match[10] || '';
if (location.hash)
location.hash = location.hash.substr(1);
match = HASH_MATCH.exec(location.hash); match = HASH_MATCH.exec(location.hash);
location.hashPath = unescape(match[1] || ''); location.hashPath = unescape(match[1] || '');
location.hashSearch = parseKeyValue(match[3]); location.hashSearch = parseKeyValue(match[3]);