Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove more code and perf wins #172

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions benchmark/parse-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,16 @@ Object.keys(top).forEach(function (domain) {
})
})

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

suite.on('cycle', function onCycle (event) {
suite.on('cycle', function (event) {
benchmarks.add(event.target)
})

suite.on('complete', function onComplete () {
suite.on('complete', function () {
benchmarks.log()
})

suite.run({async: false})

function gencookies (num) {
var str = ''

for (var i = 0; i < num; i++) {
str += '; foo' + i + '=bar'
}

return str.slice(2)
}
suite.run({ async: false })
6 changes: 3 additions & 3 deletions benchmark/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ suite.add({
fn: 'var val = cookie.parse(' + JSON.stringify(gencookies(100)) + ')'
})

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

suite.on('cycle', function onCycle (event) {
suite.on('cycle', function (event) {
benchmarks.add(event.target)
})

suite.on('complete', function onComplete () {
benchmarks.log()
})

suite.run({async: false})
suite.run({ async: false })

function gencookies (num) {
var str = ''
Expand Down
50 changes: 16 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,29 @@ var pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;
* The object has the various cookies as keys(names) => values
*
* @param {string} str
* @param {object} [options]
* @param {object} [opt]
* @return {object}
* @public
*/

function parse(str, options) {
function parse(str, opt) {
if (typeof str !== 'string') {
throw new TypeError('argument str must be a string');
}

var obj = {};
var len = str.length;
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
var max = len - 2;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was negligible and added complexity, removing.

if (max < 0) return obj;
if (len < 2) return obj;

var dec = (options && options.decode) || decode;
var dec = (opt && opt.decode) || decode;
var index = 0;
var eqIdx = 0;
var endIdx = 0;

do {
eqIdx = str.indexOf('=', index);

// no more cookie pairs
if (eqIdx === -1) {
break;
}
if (eqIdx === -1) break; // No more cookie pairs.

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

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

// only assign once
if (undefined === obj[key]) {
if (!obj.hasOwnProperty(key)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before:

> /Users/blakeembrey/.n/bin/node benchmark/parse-top.js

  cookie.parse - top sites

  15 tests completed.

  parse accounts.google.com x    12,372,758 ops/sec ±0.42% (194 runs sampled)
  parse apple.com           x    12,568,995 ops/sec ±0.72% (188 runs sampled)
  parse cloudflare.com      x    11,931,561 ops/sec ±0.35% (196 runs sampled)
  parse docs.google.com     x    11,406,060 ops/sec ±0.30% (195 runs sampled)
  parse drive.google.com    x    11,407,974 ops/sec ±0.28% (195 runs sampled)
  parse en.wikipedia.org    x     2,183,413 ops/sec ±0.43% (196 runs sampled)
  parse linkedin.com        x     2,504,458 ops/sec ±0.56% (195 runs sampled)
  parse maps.google.com     x     5,811,363 ops/sec ±0.35% (192 runs sampled)
  parse microsoft.com       x     4,165,492 ops/sec ±0.24% (193 runs sampled)
  parse play.google.com     x    11,288,231 ops/sec ±0.77% (192 runs sampled)
  parse support.google.com  x     6,936,106 ops/sec ±0.31% (195 runs sampled)
  parse www.google.com      x     4,654,382 ops/sec ±0.24% (195 runs sampled)
  parse youtu.be            x     2,150,212 ops/sec ±0.29% (191 runs sampled)
  parse youtube.com         x     2,168,764 ops/sec ±0.25% (196 runs sampled)
  parse example.com         x 1,015,740,156 ops/sec ±0.23% (196 runs sampled)

> /Users/blakeembrey/.n/bin/node benchmark/parse.js

  cookie.parse - generic

  6 tests completed.

  simple      x 14,838,760 ops/sec ±1.44% (194 runs sampled)
  decode      x  5,159,621 ops/sec ±0.37% (196 runs sampled)
  unquote     x 14,147,216 ops/sec ±0.36% (196 runs sampled)
  duplicates  x  3,946,673 ops/sec ±0.24% (197 runs sampled)
  10 cookies  x  1,159,556 ops/sec ±0.27% (196 runs sampled)
  100 cookies x     83,567 ops/sec ±0.44% (196 runs sampled)

After:

> /Users/blakeembrey/.n/bin/node benchmark/parse-top.js

  cookie.parse - top sites

  15 tests completed.

  parse accounts.google.com x     9,906,374 ops/sec ±0.40% (192 runs sampled)
  parse apple.com           x    14,517,233 ops/sec ±0.38% (195 runs sampled)
  parse cloudflare.com      x    12,549,506 ops/sec ±0.36% (195 runs sampled)
  parse docs.google.com     x    11,833,067 ops/sec ±0.28% (196 runs sampled)
  parse drive.google.com    x    11,850,144 ops/sec ±0.28% (196 runs sampled)
  parse en.wikipedia.org    x     2,243,009 ops/sec ±0.27% (193 runs sampled)
  parse linkedin.com        x     2,584,784 ops/sec ±0.28% (196 runs sampled)
  parse maps.google.com     x     6,023,326 ops/sec ±0.29% (196 runs sampled)
  parse microsoft.com       x     4,113,704 ops/sec ±0.28% (194 runs sampled)
  parse play.google.com     x    11,830,837 ops/sec ±0.28% (197 runs sampled)
  parse support.google.com  x     6,963,010 ops/sec ±0.31% (196 runs sampled)
  parse www.google.com      x     4,606,831 ops/sec ±1.37% (197 runs sampled)
  parse youtu.be            x     2,167,224 ops/sec ±0.24% (197 runs sampled)
  parse youtube.com         x     2,172,019 ops/sec ±0.28% (194 runs sampled)
  parse example.com         x 1,018,534,641 ops/sec ±0.25% (197 runs sampled)

> /Users/blakeembrey/.n/bin/node benchmark/parse.js

  cookie.parse - generic

  6 tests completed.

  simple      x 14,448,205 ops/sec ±0.46% (194 runs sampled)
  decode      x  5,128,678 ops/sec ±0.37% (193 runs sampled)
  unquote     x 13,934,772 ops/sec ±0.39% (196 runs sampled)
  duplicates  x  3,914,881 ops/sec ±0.60% (194 runs sampled)
  10 cookies  x  1,123,799 ops/sec ±0.30% (194 runs sampled)
  100 cookies x     86,013 ops/sec ±0.42% (196 runs sampled)

Hard to tell the difference over many runs, seems similar either way but hasOwnProperty would be more technically correct.

var valStartIdx = startIndex(str, eqIdx + 1, endIdx);
var valEndIdx = endIndex(str, endIdx, valStartIdx);

Expand All @@ -143,7 +138,7 @@ function parse(str, options) {
}

index = endIdx + 1
} while (index < max);
} while (index < len);

return obj;
}
Expand Down Expand Up @@ -175,14 +170,13 @@ function endIndex(str, index, min) {
*
* @param {string} name
* @param {string} val
* @param {object} [options]
* @param {object} [opt]
* @return {string}
* @public
*/

function serialize(name, val, options) {
var opt = options || {};
var enc = opt.encode || encode;
function serialize(name, val, opt) {
var enc = (opt && opt.encode) || encodeURIComponent;

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

var value = enc(val);

if (value && !cookieValueRegExp.test(value)) {
if (!cookieValueRegExp.test(value)) {
throw new TypeError('argument val is invalid');
}

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

if (null != opt.maxAge) {
var maxAge = opt.maxAge - 0;
var maxAge = Math.floor(opt.maxAge);

if (!isFinite(maxAge)) {
throw new TypeError('option maxAge is invalid')
}

str += '; Max-Age=' + Math.floor(maxAge);
str += '; Max-Age=' + maxAge;
}

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

if (opt.priority) {
var priority = typeof opt.priority === 'string'
? opt.priority.toLowerCase()
: opt.priority
? opt.priority.toLowerCase() : opt.priority;

switch (priority) {
case 'low':
Expand Down Expand Up @@ -306,17 +300,6 @@ function decode (str) {
: str
}

/**
* URL-encode value.
*
* @param {string} val
* @returns {string}
*/

function encode (val) {
return encodeURIComponent(val)
}

/**
* Determine if value is a Date.
*
Expand All @@ -325,8 +308,7 @@ function encode (val) {
*/

function isDate (val) {
return __toString.call(val) === '[object Date]' ||
val instanceof Date
return __toString.call(val) === '[object Date]';
}

/**
Expand Down
5 changes: 5 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('cookie.parse(str)', function () {

it('should parse cookie with minimum length', function () {
assert.deepEqual(cookie.parse('f='), { f: '' })
assert.deepEqual(cookie.parse('f=;b='), { f: '', b: '' })
})

it('should URL-decode values', function () {
Expand Down Expand Up @@ -67,6 +68,10 @@ describe('cookie.parse(str)', function () {
assert.deepEqual(cookie.parse('foo=false;bar=bar;foo=true'), { foo: 'false', bar: 'bar' })
assert.deepEqual(cookie.parse('foo=;bar=bar;foo=boo'), { foo: '', bar: 'bar' })
})

it('should parse native properties', function () {
assert.deepEqual(cookie.parse('toString=foo;valueOf=bar'), { toString: 'foo', valueOf: 'bar' })
})
})

describe('cookie.parse(str, options)', function () {
Expand Down
Loading