Skip to content

Commit 58015c0

Browse files
authored
Remove more code and perf wins (#172)
1 parent ab057d6 commit 58015c0

File tree

4 files changed

+28
-51
lines changed

4 files changed

+28
-51
lines changed

benchmark/parse-top.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,16 @@ Object.keys(top).forEach(function (domain) {
2222
})
2323
})
2424

25-
suite.on('start', function onCycle (event) {
25+
suite.on('start', function () {
2626
process.stdout.write(' cookie.parse - top sites\n\n')
2727
})
2828

29-
suite.on('cycle', function onCycle (event) {
29+
suite.on('cycle', function (event) {
3030
benchmarks.add(event.target)
3131
})
3232

33-
suite.on('complete', function onComplete () {
33+
suite.on('complete', function () {
3434
benchmarks.log()
3535
})
3636

37-
suite.run({async: false})
38-
39-
function gencookies (num) {
40-
var str = ''
41-
42-
for (var i = 0; i < num; i++) {
43-
str += '; foo' + i + '=bar'
44-
}
45-
46-
return str.slice(2)
47-
}
37+
suite.run({ async: false })

benchmark/parse.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ suite.add({
4949
fn: 'var val = cookie.parse(' + JSON.stringify(gencookies(100)) + ')'
5050
})
5151

52-
suite.on('start', function onCycle (event) {
52+
suite.on('start', function () {
5353
process.stdout.write(' cookie.parse - generic\n\n')
5454
})
5555

56-
suite.on('cycle', function onCycle (event) {
56+
suite.on('cycle', function (event) {
5757
benchmarks.add(event.target)
5858
})
5959

6060
suite.on('complete', function onComplete () {
6161
benchmarks.log()
6262
})
6363

64-
suite.run({async: false})
64+
suite.run({ async: false })
6565

6666
function gencookies (num) {
6767
var str = ''

index.js

+16-34
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,29 @@ var pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;
8585
* The object has the various cookies as keys(names) => values
8686
*
8787
* @param {string} str
88-
* @param {object} [options]
88+
* @param {object} [opt]
8989
* @return {object}
9090
* @public
9191
*/
9292

93-
function parse(str, options) {
93+
function parse(str, opt) {
9494
if (typeof str !== 'string') {
9595
throw new TypeError('argument str must be a string');
9696
}
9797

9898
var obj = {};
9999
var len = str.length;
100100
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
101-
var max = len - 2;
102-
if (max < 0) return obj;
101+
if (len < 2) return obj;
103102

104-
var dec = (options && options.decode) || decode;
103+
var dec = (opt && opt.decode) || decode;
105104
var index = 0;
106105
var eqIdx = 0;
107106
var endIdx = 0;
108107

109108
do {
110109
eqIdx = str.indexOf('=', index);
111-
112-
// no more cookie pairs
113-
if (eqIdx === -1) {
114-
break;
115-
}
110+
if (eqIdx === -1) break; // No more cookie pairs.
116111

117112
endIdx = str.indexOf(';', index);
118113

@@ -129,7 +124,7 @@ function parse(str, options) {
129124
var key = str.slice(keyStartIdx, keyEndIdx);
130125

131126
// only assign once
132-
if (undefined === obj[key]) {
127+
if (!obj.hasOwnProperty(key)) {
133128
var valStartIdx = startIndex(str, eqIdx + 1, endIdx);
134129
var valEndIdx = endIndex(str, endIdx, valStartIdx);
135130

@@ -143,7 +138,7 @@ function parse(str, options) {
143138
}
144139

145140
index = endIdx + 1
146-
} while (index < max);
141+
} while (index < len);
147142

148143
return obj;
149144
}
@@ -175,14 +170,13 @@ function endIndex(str, index, min) {
175170
*
176171
* @param {string} name
177172
* @param {string} val
178-
* @param {object} [options]
173+
* @param {object} [opt]
179174
* @return {string}
180175
* @public
181176
*/
182177

183-
function serialize(name, val, options) {
184-
var opt = options || {};
185-
var enc = opt.encode || encode;
178+
function serialize(name, val, opt) {
179+
var enc = (opt && opt.encode) || encodeURIComponent;
186180

187181
if (typeof enc !== 'function') {
188182
throw new TypeError('option encode is invalid');
@@ -194,20 +188,21 @@ function serialize(name, val, options) {
194188

195189
var value = enc(val);
196190

197-
if (value && !cookieValueRegExp.test(value)) {
191+
if (!cookieValueRegExp.test(value)) {
198192
throw new TypeError('argument val is invalid');
199193
}
200194

201195
var str = name + '=' + value;
196+
if (!opt) return str;
202197

203198
if (null != opt.maxAge) {
204-
var maxAge = opt.maxAge - 0;
199+
var maxAge = Math.floor(opt.maxAge);
205200

206201
if (!isFinite(maxAge)) {
207202
throw new TypeError('option maxAge is invalid')
208203
}
209204

210-
str += '; Max-Age=' + Math.floor(maxAge);
205+
str += '; Max-Age=' + maxAge;
211206
}
212207

213208
if (opt.domain) {
@@ -250,8 +245,7 @@ function serialize(name, val, options) {
250245

251246
if (opt.priority) {
252247
var priority = typeof opt.priority === 'string'
253-
? opt.priority.toLowerCase()
254-
: opt.priority
248+
? opt.priority.toLowerCase() : opt.priority;
255249

256250
switch (priority) {
257251
case 'low':
@@ -306,17 +300,6 @@ function decode (str) {
306300
: str
307301
}
308302

309-
/**
310-
* URL-encode value.
311-
*
312-
* @param {string} val
313-
* @returns {string}
314-
*/
315-
316-
function encode (val) {
317-
return encodeURIComponent(val)
318-
}
319-
320303
/**
321304
* Determine if value is a Date.
322305
*
@@ -325,8 +308,7 @@ function encode (val) {
325308
*/
326309

327310
function isDate (val) {
328-
return __toString.call(val) === '[object Date]' ||
329-
val instanceof Date
311+
return __toString.call(val) === '[object Date]';
330312
}
331313

332314
/**

test/parse.js

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('cookie.parse(str)', function () {
2929

3030
it('should parse cookie with minimum length', function () {
3131
assert.deepEqual(cookie.parse('f='), { f: '' })
32+
assert.deepEqual(cookie.parse('f=;b='), { f: '', b: '' })
3233
})
3334

3435
it('should URL-decode values', function () {
@@ -67,6 +68,10 @@ describe('cookie.parse(str)', function () {
6768
assert.deepEqual(cookie.parse('foo=false;bar=bar;foo=true'), { foo: 'false', bar: 'bar' })
6869
assert.deepEqual(cookie.parse('foo=;bar=bar;foo=boo'), { foo: '', bar: 'bar' })
6970
})
71+
72+
it('should parse native properties', function () {
73+
assert.deepEqual(cookie.parse('toString=foo;valueOf=bar'), { toString: 'foo', valueOf: 'bar' })
74+
})
7075
})
7176

7277
describe('cookie.parse(str, options)', function () {

0 commit comments

Comments
 (0)