Skip to content

Commit

Permalink
perf: improve header parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 15, 2017
1 parent ad1efea commit d469116
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* perf: improve header parsing
* perf: reduce overhead when no `X-Forwarded-For` header

0.1.1 / 2017-09-10
Expand Down
42 changes: 28 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

'use strict'

/**
* Simple expression to split token list.
* @private
*/

var TOKEN_LIST_REGEXP = / *, */

/**
* Module exports.
* @public
Expand Down Expand Up @@ -50,13 +43,34 @@ function forwarded (req) {
*/

function parse (header) {
if (!header) {
return []
var end = header.length
var list = []
var start = header.length

// gather addresses, backwards
for (var i = header.length - 1; i >= 0; i--) {
switch (header.charCodeAt(i)) {
case 0x20: /* */
if (start === end) {
start = end = i
}
break
case 0x2c: /* , */
if (start !== end) {
list.push(header.substring(start, end))
}
start = end = i
break
default:
start = i
break
}
}

// final address
if (start !== end) {
list.push(header.substring(start, end))
}

return header
.trim()
.split(TOKEN_LIST_REGEXP)
.filter(Boolean)
.reverse()
return list
}

0 comments on commit d469116

Please sign in to comment.