Skip to content

Commit 21a0f0c

Browse files
committed
perf: improve If-None-Match token parsing
1 parent ff5f257 commit 21a0f0c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==========
33

44
* Fix regression matching multiple ETags in `If-None-Match`
5+
* perf: improve `If-None-Match` token parsing
56

67
0.5.1 / 2017-09-11
78
==================

index.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414

1515
var CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/
1616

17-
/**
18-
* Simple expression to split token list.
19-
* @private
20-
*/
21-
22-
var TOKEN_LIST_REGEXP = / *, */
23-
2417
/**
2518
* Module exports.
2619
* @public
@@ -64,7 +57,7 @@ function fresh (reqHeaders, resHeaders) {
6457
}
6558

6659
var etagStale = true
67-
var matches = noneMatch.split(TOKEN_LIST_REGEXP)
60+
var matches = parseTokenList(noneMatch)
6861
for (var i = 0; i < matches.length; i++) {
6962
var match = matches[i]
7063
if (match === etag || match === 'W/' + etag || 'W/' + match === etag) {
@@ -106,3 +99,39 @@ function parseHttpDate (date) {
10699
? timestamp
107100
: NaN
108101
}
102+
103+
/**
104+
* Parse a HTTP token list.
105+
*
106+
* @param {string} str
107+
* @private
108+
*/
109+
110+
function parseTokenList (str) {
111+
var end = 0
112+
var list = []
113+
var start = 0
114+
115+
// gather tokens
116+
for (var i = 0, len = str.length; i < len; i++) {
117+
switch (str.charCodeAt(i)) {
118+
case 0x20: /* */
119+
if (start === end) {
120+
start = end = i + 1
121+
}
122+
break
123+
case 0x2c: /* , */
124+
list.push(str.substring(start, end))
125+
start = end = i + 1
126+
break
127+
default:
128+
end = i + 1
129+
break
130+
}
131+
}
132+
133+
// final token
134+
list.push(str.substring(start, end))
135+
136+
return list
137+
}

0 commit comments

Comments
 (0)