Skip to content

Commit

Permalink
perf: reduce loop count in header parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Dec 18, 2014
1 parent 6bd5402 commit 4c368e7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
19 changes: 14 additions & 5 deletions lib/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ module.exports = preferredCharsets;
preferredCharsets.preferredCharsets = preferredCharsets;

function parseAcceptCharset(accept) {
return accept.split(',').map(function(e, i) {
return parseCharset(e.trim(), i);
}).filter(function(e) {
return e;
});
var accepts = accept.split(',');

for (var i = 0, j = 0; i < accepts.length; i++) {
var charset = parseCharset(accepts[i].trim(), i);

if (charset) {
accepts[j++] = charset;
}
}

// trim accepts
accepts.length = j;

return accepts;
}

function parseCharset(s, i) {
Expand Down
44 changes: 19 additions & 25 deletions lib/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,35 @@ module.exports = preferredEncodings;
preferredEncodings.preferredEncodings = preferredEncodings;

function parseAcceptEncoding(accept) {
var acceptableEncodings;
var accepts = accept.split(',');
var hasIdentity = false;
var minQuality = 1;

if (accept) {
acceptableEncodings = accept.split(',').map(function(e, i) {
return parseEncoding(e.trim(), i);
});
} else {
acceptableEncodings = [];
for (var i = 0, j = 0; i < accepts.length; i++) {
var encoding = parseEncoding(accepts[i].trim(), i);

if (encoding) {
accepts[j++] = encoding;
hasIdentity = hasIdentity || specify('identity', encoding);
minQuality = Math.min(minQuality, encoding.q);
}
}

if (!acceptableEncodings.some(function(e) {
return e && specify('identity', e);
})) {
if (!hasIdentity) {
/*
* If identity doesn't explicitly appear in the accept-encoding header,
* it's added to the list of acceptable encoding with the lowest q
*
*/
var lowestQ = 1;

for(var i = 0; i < acceptableEncodings.length; i++){
var e = acceptableEncodings[i];
if(e && e.q < lowestQ){
lowestQ = e.q;
}
}
acceptableEncodings.push({
accepts[j++] = {
encoding: 'identity',
q: lowestQ / 2,
});
q: minQuality / 2
};
}

return acceptableEncodings.filter(function(e) {
return e;
});
// trim accepts
accepts.length = j;

return accepts;
}

function parseEncoding(s, i) {
Expand Down
19 changes: 14 additions & 5 deletions lib/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ module.exports = preferredLanguages;
preferredLanguages.preferredLanguages = preferredLanguages;

function parseAcceptLanguage(accept) {
return accept.split(',').map(function(e, i) {
return parseLanguage(e.trim(), i);
}).filter(function(e) {
return e;
});
var accepts = accept.split(',');

for (var i = 0, j = 0; i < accepts.length; i++) {
var langauge = parseLanguage(accepts[i].trim(), i);

if (langauge) {
accepts[j++] = langauge;
}
}

// trim accepts
accepts.length = j;

return accepts;
}

function parseLanguage(s, i) {
Expand Down
19 changes: 14 additions & 5 deletions lib/mediaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ module.exports = preferredMediaTypes;
preferredMediaTypes.preferredMediaTypes = preferredMediaTypes;

function parseAccept(accept) {
return accept.split(',').map(function(e, i) {
return parseMediaType(e.trim(), i);
}).filter(function(e) {
return e;
});
var accepts = accept.split(',');

for (var i = 0, j = 0; i < accepts.length; i++) {
var mediaType = parseMediaType(accepts[i].trim(), i);

if (mediaType) {
accepts[j++] = mediaType;
}
}

// trim accepts
accepts.length = j;

return accepts;
};

function parseMediaType(s, i) {
Expand Down

0 comments on commit 4c368e7

Please sign in to comment.