Skip to content

Conversation

@hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Jan 29, 2026

Description

I compared three serialization library.

Here is a benchmark of large blob report serialization (~500 test files, 50000 tests). See https://github.com/hi-ogawa/reproductions/tree/main/vitest-9549-serializer for details.

(Note that @ungap/structured-clone's data isn't generated through Vitest, but it simply tests equivalent data by converting from flatted result.)

 ✓ demo.bench.ts > parse 18821ms
     name                         hz      min     max    mean     p75     p99    p995    p999     rme  samples
   · flatted                  1.4627   661.73  724.88  683.68  699.01  724.88  724.88  724.88  ±1.96%       10
   · devalue                  8.9283  97.1357  125.28  112.00  114.71  125.28  125.28  125.28  ±4.62%       10
   · @ungap/structured-clone  3.5498   247.89  353.75  281.71  289.40  353.75  353.75  353.75  ±8.10%       10

 ✓ demo.bench.ts > stringify 17660ms
     name                         hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · flatted                  3.6988  252.01  289.50  270.36  277.55  289.50  289.50  289.50  ±2.86%       10
   · devalue                  2.1953  423.41  485.84  455.51  471.23  485.84  485.84  485.84  ±3.29%       10
   · @ungap/structured-clone  3.1401  278.85  391.83  318.46  338.29  391.83  391.83  391.83  ±8.20%       10

 BENCH  Summary
  devalue - demo.bench.ts > parse
    2.52x faster than @ungap/structured-clone
    6.10x faster than flatted
  flatted - demo.bench.ts > stringify
    1.18x faster than @ungap/structured-clone
    1.68x faster than devalue

and blob file size comparison:

Serializer Size Gzip size
flatted 22M 2.14 MB
devalue 23M 2.62 MB
@ungap/structured-clone 18M 2.45 MB

It appears that flatted's simple implementation has some edge and it's not like modern-ish alternative wins absolutely. For Vitest use case, there's probably no crystal clear reason to replace flatted at the moment, other than perhaps building our own serializer to pick up the good parts of all. If I would choose one based on my test, I think I'd choose devalue as it's most battle tested and most flexible, but not sure what others think.

Here are some thoughts on options (and non options)

devalue

  • pros
    • fastest parse
    • non-json type support
    • custom replacer/reviver support
  • cons
    • larger size: this is likely because undefined key value is included. otherwise smaller than flatted. it's should be possible to omit these from custom replacer, but haven't tried.
    • slowest stringify
    • reject unsupported type and symbols by default, so Vitest needs custom replacer/reviver to normalize make them into plain objects.

@ungap/structured-clone

  • pros
    • smaller size
    • parse faster than flatted
    • non-json type support like structureClone (though Vitest doesn't use them)
  • cons
    • stringify slower than flatted
    • no custom replacer/reviver like flatted (Vitest needs minor customization on Error instance)

other options (e.g. seroval, turbo-stream)

  • cons
    • untested, but I expect streaming/async serialization will have clear perf loss for Vitest's synchronous data use case.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

hi-ogawa and others added 2 commits January 29, 2026 16:48
- Add new serialization module to @vitest/utils with parse, stringify,
  stringifyRaw, stringifyReplace, and toJSON exports
- Migrate all flatted imports to use @vitest/utils/serialization
- Remove duplicate stringifyReplace implementations
- Consistent Error serialization across all usages

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@netlify
Copy link

netlify bot commented Jan 29, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 11b24f0
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/698051b090576700086b2ef5
😎 Deploy Preview https://deploy-preview-9549--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@hi-ogawa hi-ogawa changed the title refactor(utils): consolidate flatted into @vitest/utils/serialization perf: replace flatted with devalue for serialization Feb 2, 2026
@hi-ogawa hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Feb 2, 2026
@hi-ogawa hi-ogawa moved this to P2 - 3 in Team Board Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-to-be-discussed Enhancement under consideration (priority)

Projects

Status: P2 - 3

Development

Successfully merging this pull request may close these issues.

1 participant