Skip to content

Commit

Permalink
optimize res.format()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 23, 2012
1 parent 812a470 commit 533b312
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,22 @@ res.format = function(obj){
, req = this.req
, next = req.next
, accepted = req.accepted
, acceptedlen = accepted.length
, type
, key;

// normalize extnames -> mime
for (var i = 0; i < keys.length; ++i) {
types.push(~keys[i].indexOf('/')
? keys[i]
: mime.lookup(keys[i]));
if (acceptedlen) {
for (var i = 0; i < keys.length; ++i) {
types.push(~keys[i].indexOf('/')
? keys[i]
: mime.lookup(keys[i]));
}
}

// determine most acceptable format
out:
for (var i = 0, len = accepted.length; i < len; ++i) {
for (var i = 0; i < acceptedlen; ++i) {
for (var j = 0, jlen = types.length; j < jlen; ++j) {
if (accept(types[j].split('/'), accepted[i])) {
key = keys[j];
Expand All @@ -367,7 +370,7 @@ res.format = function(obj){
}

// default to the first
if (!accepted.length) {
if (!acceptedlen) {
key = keys[0];
type = types[0];
}
Expand Down

0 comments on commit 533b312

Please sign in to comment.