jshttp / cookie

HTTP server cookie parsing and serialization
MIT License
1.36k stars 159 forks source link

Iterate whitespace for perf #170

Closed blakeembrey closed 1 month ago

blakeembrey commented 1 month ago

Uses a loop over spaces instead of .trim(). Appears to be a reasonable perf improvement in testing.

Before:

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

  cookie.parse - top sites

  14 tests completed.

  parse accounts.google.com x 10,690,166 ops/sec ±0.55% (194 runs sampled)
  parse apple.com           x 12,183,219 ops/sec ±0.31% (197 runs sampled)
  parse cloudflare.com      x 10,442,218 ops/sec ±0.28% (197 runs sampled)
  parse docs.google.com     x 10,084,253 ops/sec ±0.26% (196 runs sampled)
  parse drive.google.com    x 10,124,351 ops/sec ±0.22% (196 runs sampled)
  parse en.wikipedia.org    x  1,942,194 ops/sec ±0.23% (189 runs sampled)
  parse linkedin.com        x  2,183,254 ops/sec ±0.24% (193 runs sampled)
  parse maps.google.com     x  5,293,295 ops/sec ±0.23% (196 runs sampled)
  parse microsoft.com       x  3,659,868 ops/sec ±0.24% (195 runs sampled)
  parse play.google.com     x 10,503,304 ops/sec ±0.55% (197 runs sampled)
  parse support.google.com  x  6,390,620 ops/sec ±1.16% (194 runs sampled)
  parse www.google.com      x  4,176,170 ops/sec ±0.23% (195 runs sampled)
  parse youtu.be            x  2,023,661 ops/sec ±0.53% (195 runs sampled)
  parse youtube.com         x  2,032,070 ops/sec ±0.22% (197 runs sampled)

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

  cookie.parse - generic

  6 tests completed.

  simple      x 13,431,777 ops/sec ±0.33% (189 runs sampled)
  decode      x  4,964,245 ops/sec ±0.29% (195 runs sampled)
  unquote     x 12,254,744 ops/sec ±0.31% (196 runs sampled)
  duplicates  x  3,428,104 ops/sec ±0.28% (195 runs sampled)
  10 cookies  x    953,884 ops/sec ±0.27% (195 runs sampled)
  100 cookies x     74,344 ops/sec ±0.57% (192 runs sampled)

After:

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

  cookie.parse - top sites

  14 tests completed.

  parse accounts.google.com x 11,760,349 ops/sec ±0.36% (190 runs sampled)
  parse apple.com           x 13,340,320 ops/sec ±0.36% (193 runs sampled)
  parse cloudflare.com      x 11,739,042 ops/sec ±0.27% (196 runs sampled)
  parse docs.google.com     x 11,279,592 ops/sec ±0.21% (195 runs sampled)
  parse drive.google.com    x 11,118,106 ops/sec ±0.21% (195 runs sampled)
  parse en.wikipedia.org    x  2,269,686 ops/sec ±0.22% (196 runs sampled)
  parse linkedin.com        x  2,602,998 ops/sec ±1.28% (191 runs sampled)
  parse maps.google.com     x  6,016,290 ops/sec ±0.26% (192 runs sampled)
  parse microsoft.com       x  4,372,931 ops/sec ±0.43% (195 runs sampled)
  parse play.google.com     x 11,836,388 ops/sec ±0.18% (195 runs sampled)
  parse support.google.com  x  7,260,207 ops/sec ±0.27% (197 runs sampled)
  parse www.google.com      x  4,770,988 ops/sec ±0.20% (197 runs sampled)
  parse youtu.be            x  2,229,027 ops/sec ±0.24% (196 runs sampled)
  parse youtube.com         x  2,234,865 ops/sec ±0.22% (196 runs sampled)

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

  cookie.parse - generic

  6 tests completed.

  simple      x 15,129,601 ops/sec ±0.33% (193 runs sampled)
  decode      x  5,200,520 ops/sec ±0.33% (197 runs sampled)
  unquote     x 14,504,769 ops/sec ±0.32% (197 runs sampled)
  duplicates  x  4,088,642 ops/sec ±0.29% (195 runs sampled)
  10 cookies  x  1,182,534 ops/sec ±0.25% (194 runs sampled)
  100 cookies x     82,411 ops/sec ±0.55% (196 runs sampled)
blakeembrey commented 1 month ago

Using a loop to find = adds another small bump (in most cases):

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

  cookie.parse - top sites

  14 tests completed.

  parse accounts.google.com x 12,036,730 ops/sec ±0.39% (193 runs sampled)
  parse apple.com           x 14,565,865 ops/sec ±0.38% (196 runs sampled)
  parse cloudflare.com      x 11,998,435 ops/sec ±0.36% (195 runs sampled)
  parse docs.google.com     x 11,210,175 ops/sec ±0.19% (196 runs sampled)
  parse drive.google.com    x 11,201,061 ops/sec ±0.23% (196 runs sampled)
  parse en.wikipedia.org    x  2,125,988 ops/sec ±0.26% (194 runs sampled)
  parse linkedin.com        x  2,512,798 ops/sec ±0.21% (195 runs sampled)
  parse maps.google.com     x  6,320,705 ops/sec ±0.26% (195 runs sampled)
  parse microsoft.com       x  4,206,180 ops/sec ±0.23% (195 runs sampled)
  parse play.google.com     x 12,477,047 ops/sec ±1.11% (190 runs sampled)
  parse support.google.com  x  7,708,796 ops/sec ±0.38% (193 runs sampled)
  parse www.google.com      x  4,982,141 ops/sec ±0.39% (196 runs sampled)
  parse youtu.be            x  2,134,174 ops/sec ±0.21% (196 runs sampled)
  parse youtube.com         x  2,138,205 ops/sec ±0.25% (196 runs sampled)

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

  cookie.parse - generic

  6 tests completed.

  simple      x 16,037,488 ops/sec ±0.61% (193 runs sampled)
  decode      x  5,224,018 ops/sec ±0.39% (195 runs sampled)
  unquote     x 14,508,555 ops/sec ±0.43% (197 runs sampled)
  duplicates  x  3,992,515 ops/sec ±0.42% (195 runs sampled)
  10 cookies  x  1,159,203 ops/sec ±0.26% (196 runs sampled)
  100 cookies x     81,236 ops/sec ±0.54% (193 runs sampled)
blakeembrey commented 1 month ago

Undid the = loop for now since perf falls behind after ~10 characters, so it'd be a matter of deciding whether most cookie names are < 10 characters or not to adopt that change.