Skip to content

fix: add string flattening to the benchmarks - #894

Open
ivan-tymoshenko wants to merge 1 commit into
mainfrom
update-benchmarks
Open

ivan-tymoshenko wants to merge 1 commit into
mainfrom
update-benchmarks

Conversation

@ivan-tymoshenko

Copy link
Copy Markdown
Member

The benchmark needs to consume the resulting string

One important detail is that returning a string does not necessarily mean all the work of building it has happened.

FJS returns an unflattened string (a tree of references to smaller string pieces, also called a rope). Native JSON.stringify returns a flat string, with its characters already stored contiguously. Concatenation can therefore look very cheap in a serialization-only benchmark because some copying is deferred.

I updated the benchmark to measure:

const json = serialize(object)
return Buffer.byteLength(json, 'utf8')

This includes serialization and byte length counting, without route or socket overhead. It does not include actual UTF-8 output encoding or the complete HTTP response path.

!! Please take a look at the last 3 bench cases that shows the real world responses.
*all the benchmarks includes unmerged optimization: #886

Results for nodejs 24.

❯ /home/ivan/.nvm/versions/node/v24.15.0/bin/node --expose-gc benchmark/bench-cmp-lib.js --fjs-vs-json

--- creation and first stringify ---
🏆 fast-json-stringify                      x          23,443 ops/sec ±0.25% (23185 runs sampled)

--- array ---
🏆 JSON.stringify                           x          17,366 ops/sec ±0.07% (17353 runs sampled)
   fast-json-stringify [default]            x          14,019 ops/sec ±0.30% (13870 runs sampled)
   fast-json-stringify [json-stringify]     x           8,218 ops/sec ±0.24% (8176 runs sampled)

--- large array ---
🏆 fast-json-stringify [json-stringify]     x             739 ops/sec ±1.05% (731 runs sampled)
   JSON.stringify                           x             730 ops/sec ±1.00% (721 runs sampled)
   fast-json-stringify [default]            x             565 ops/sec ±1.78% (549 runs sampled)

--- long string ---
🏆 JSON.stringify                           x          60,448 ops/sec ±0.25% (59416 runs sampled)
   fast-json-stringify                      x          60,064 ops/sec ±0.14% (58760 runs sampled)

--- short string ---
🏆 fast-json-stringify                      x      23,281,901 ops/sec ±0.02% (22846203 runs sampled)
   JSON.stringify                           x      12,507,725 ops/sec ±0.13% (12328835 runs sampled)

--- obj ---
🏆 JSON.stringify                           x       6,879,736 ops/sec ±4.96% (6646696 runs sampled)
   fast-json-stringify                      x       6,203,188 ops/sec ±19.69% (5533413 runs sampled)

--- date ---
🏆 fast-json-stringify                      x       2,706,689 ops/sec ±0.01% (2701141 runs sampled)
   JSON.stringify                           x       1,550,297 ops/sec ±9.17% (1475123 runs sampled)

--- user profile response ---
🏆 JSON.stringify                           x         853,691 ops/sec ±0.10% (848898 runs sampled)
   fast-json-stringify                      x         435,157 ops/sec ±1.28% (431065 runs sampled)

--- order response ---
🏆 JSON.stringify                           x         166,853 ops/sec ±0.07% (166206 runs sampled)
   fast-json-stringify                      x          82,633 ops/sec ±0.29% (82337 runs sampled)

--- paginated products response ---
🏆 JSON.stringify                           x          22,363 ops/sec ±0.09% (22301 runs sampled)
   fast-json-stringify                      x           9,978 ops/sec ±0.11% (9961 runs sampled)

Results for nodejs 26

❯ /home/ivan/.nvm/versions/node/v26.10.0/bin/node --expose-gc benchmark/bench-cmp-lib.js --fjs-vs-json

--- creation and first stringify ---
🏆 fast-json-stringify                      x          27,081 ops/sec ±0.34% (26624 runs sampled)

--- array ---
🏆 JSON.stringify                           x          33,730 ops/sec ±0.10% (33655 runs sampled)
   fast-json-stringify [default]            x          29,911 ops/sec ±0.12% (29781 runs sampled)
   fast-json-stringify [json-stringify]     x           9,420 ops/sec ±0.38% (9289 runs sampled)

--- large array ---
🏆 JSON.stringify                           x           1,470 ops/sec ±0.46% (1460 runs sampled)
   fast-json-stringify [json-stringify]     x           1,442 ops/sec ±0.46% (1433 runs sampled)
   fast-json-stringify [default]            x           1,270 ops/sec ±1.03% (1247 runs sampled)

--- long string ---
🏆 fast-json-stringify                      x          82,467 ops/sec ±0.13% (82023 runs sampled)
   JSON.stringify                           x          82,200 ops/sec ±0.13% (81773 runs sampled)

--- short string ---
🏆 JSON.stringify                           x      19,301,338 ops/sec ±0.39% (19074572 runs sampled)
   fast-json-stringify                      x      13,977,038 ops/sec ±0.08% (13906866 runs sampled)

--- obj ---
🏆 JSON.stringify                           x      11,413,950 ops/sec ±1.49% (11241825 runs sampled)
   fast-json-stringify                      x       6,479,504 ops/sec ±1.17% (6388408 runs sampled)

--- date ---
🏆 fast-json-stringify                      x       2,993,286 ops/sec ±0.06% (2985258 runs sampled)
   JSON.stringify                           x       1,632,403 ops/sec ±9.90% (1547023 runs sampled)

--- user profile response ---
🏆 JSON.stringify                           x       1,838,032 ops/sec ±6.12% (1746321 runs sampled)
   fast-json-stringify                      x         491,409 ops/sec ±0.12% (489067 runs sampled)

--- order response ---
🏆 JSON.stringify                           x         221,118 ops/sec ±1.03% (218773 runs sampled)
   fast-json-stringify                      x          93,830 ops/sec ±0.41% (93077 runs sampled)

--- paginated products response ---
🏆 JSON.stringify                           x          33,861 ops/sec ±0.11% (33767 runs sampled)
   fast-json-stringify                      x          10,929 ops/sec ±0.88% (10823 runs sampled)

@gurgunday gurgunday left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

This branch has not been deployed

No deployments
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