Skip to content

perf: make stringifySetCookie 37% faster for Node18/Node20#268

Merged
blakeembrey merged 1 commit intojshttp:masterfrom
saripovdenis:perf/stringify
May 7, 2026
Merged

perf: make stringifySetCookie 37% faster for Node18/Node20#268
blakeembrey merged 1 commit intojshttp:masterfrom
saripovdenis:perf/stringify

Conversation

@saripovdenis
Copy link
Copy Markdown
Contributor

Avoid creating a temporary cookie object for the stringifySetCookie(name, value, options) overload.

Previously this path copied options into a new object:

{ ..._opts, name: _name, value: String(_val) }

The function then immediately read those values back. This change reads name, value, and options directly, avoiding the object spread and extra allocation.

The no-options simple case stayed roughly flat in local runs. The main win is for option-heavy and repeated Set-Cookie serialization.

Benchmark:

npm run bench -- src/stringify-set-cookie.bench.ts

Case Before ops/sec After ops/sec Improvement
attributes 557,937.36 2,434,022.10 +336.25%
10 set-cookies 573,730.84 782,330.30 +36.36%
100 set-cookies 56,280.08 77,878.66 +38.38%

Validation:

  • npm test
  • npm run bench -- src/stringify-set-cookie.bench.ts

@blakeembrey
Copy link
Copy Markdown
Member

I don't see the stated change in performance when comparing this PR to main, which is what I expect since the benchmarks are based on the object format which doesn't spread. Only attributes should be improved, but that seems negligible:

 ✓ src/stringify-set-cookie.bench.ts > cookie.stringifySetCookie 6426ms
     name                       hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · simple           7,467,847.64  0.0000  0.2532  0.0001  0.0001  0.0002  0.0002  0.0006  ±0.24%  3733924
   · encode           5,751,041.95  0.0001  0.0388  0.0002  0.0002  0.0002  0.0002  0.0003  ±0.06%  2875521
   · attributes       1,463,595.65  0.0005  0.2633  0.0007  0.0007  0.0008  0.0008  0.0013  ±0.28%   731798
   · object input     5,269,089.54  0.0001  0.3438  0.0002  0.0002  0.0003  0.0003  0.0009  ±0.35%  2634545
   · 10 set-cookies     512,368.76  0.0018  0.2956  0.0020  0.0019  0.0023  0.0024  0.0046  ±0.37%   256185
   · 100 set-cookies     51,604.82  0.0180  1.1578  0.0194  0.0193  0.0248  0.0309  0.0705  ±0.77%    25803

This PR:

 ✓ src/stringify-set-cookie.bench.ts > cookie.stringifySetCookie 6607ms
     name                       hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · simple           7,604,392.31  0.0000  1.7841  0.0001  0.0001  0.0002  0.0002  0.0003  ±0.71%  3802197
   · encode           5,910,409.57  0.0001  0.0674  0.0002  0.0002  0.0002  0.0002  0.0003  ±0.07%  2955205
   · attributes       1,530,590.33  0.0005  0.2714  0.0007  0.0007  0.0007  0.0008  0.0012  ±0.26%   765296
   · object input     5,449,671.96  0.0001  0.3300  0.0002  0.0002  0.0002  0.0002  0.0005  ±0.33%  2724836
   · 10 set-cookies     521,176.53  0.0017  9.4774  0.0019  0.0019  0.0022  0.0023  0.0025  ±3.72%   260589
   · 100 set-cookies     55,729.41  0.0175  0.3021  0.0179  0.0178  0.0203  0.0229  0.0477  ±0.27%    27865

@saripovdenis saripovdenis changed the title perf: make stringifySetCookie 37% faster perf: make stringifySetCookie 37% faster for Node18/Node20 Apr 28, 2026
@saripovdenis
Copy link
Copy Markdown
Contributor Author

I don't see the stated change in performance when comparing this PR to main, which is what I expect since the benchmarks are based on the object format which doesn't spread. Only attributes should be improved, but that seems negligible:

 ✓ src/stringify-set-cookie.bench.ts > cookie.stringifySetCookie 6426ms
     name                       hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · simple           7,467,847.64  0.0000  0.2532  0.0001  0.0001  0.0002  0.0002  0.0006  ±0.24%  3733924
   · encode           5,751,041.95  0.0001  0.0388  0.0002  0.0002  0.0002  0.0002  0.0003  ±0.06%  2875521
   · attributes       1,463,595.65  0.0005  0.2633  0.0007  0.0007  0.0008  0.0008  0.0013  ±0.28%   731798
   · object input     5,269,089.54  0.0001  0.3438  0.0002  0.0002  0.0003  0.0003  0.0009  ±0.35%  2634545
   · 10 set-cookies     512,368.76  0.0018  0.2956  0.0020  0.0019  0.0023  0.0024  0.0046  ±0.37%   256185
   · 100 set-cookies     51,604.82  0.0180  1.1578  0.0194  0.0193  0.0248  0.0309  0.0705  ±0.77%    25803

This PR:

 ✓ src/stringify-set-cookie.bench.ts > cookie.stringifySetCookie 6607ms
     name                       hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · simple           7,604,392.31  0.0000  1.7841  0.0001  0.0001  0.0002  0.0002  0.0003  ±0.71%  3802197
   · encode           5,910,409.57  0.0001  0.0674  0.0002  0.0002  0.0002  0.0002  0.0003  ±0.07%  2955205
   · attributes       1,530,590.33  0.0005  0.2714  0.0007  0.0007  0.0007  0.0008  0.0012  ±0.26%   765296
   · object input     5,449,671.96  0.0001  0.3300  0.0002  0.0002  0.0002  0.0002  0.0005  ±0.33%  2724836
   · 10 set-cookies     521,176.53  0.0017  9.4774  0.0019  0.0019  0.0022  0.0023  0.0025  ±3.72%   260589
   · 100 set-cookies     55,729.41  0.0175  0.3021  0.0179  0.0178  0.0203  0.0229  0.0477  ±0.27%    27865

oh, i got the reason - i was running benchmarks on node20, while you were running on newer version

This optimisation still makes sense for node20 (and node 18), while newer node versions are neutral

What do you think @blakeembrey ?

@blakeembrey
Copy link
Copy Markdown
Member

Yep, I agree, we should land it if there's a large perf hit on those older node versions.

In the next major this code path will be removed in favor of the object mode, as well as we'll likely bump node to min 22 or 24.

@saripovdenis
Copy link
Copy Markdown
Contributor Author

Yep, I agree, we should land it if there's a large perf hit on those older node versions.

In the next major this code path will be removed in favor of the object mode, as well as we'll likely bump node to min 22 or 24.

Great! Is there anything you want me to add here?

@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (4898ba2) to head (17149f5).
⚠️ Report is 26 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #268   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          160       256   +96     
  Branches        69       116   +47     
=========================================
+ Hits           160       256   +96     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blakeembrey blakeembrey merged commit 15a1d8c into jshttp:master May 7, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants