⚡️ Faster fc.record on generate#7046
Merged
Merged
Conversation
Build generated records with direct property assignment instead of
Object.defineProperty in the values-to-object mapper.
Object.defineProperty with { configurable, enumerable, writable } all set
to true produces exactly the same own data property as a plain
assignment, but is significantly slower in V8 and pushes the freshly
created object onto a slower path. Switching to assignment keeps the
object on the fast path and avoids the per-property builtin call.
The only key that cannot use a plain assignment is the string
"__proto__" (it would trigger the prototype setter on a regular object
instead of defining an own property), so it keeps using defineProperty.
Symbol keys and every other string key are assigned directly.
Isolated mapper microbenchmark (objects/sec, higher is better):
- 1 key: ~356 ns/op -> ~15 ns/op
- 3 keys: ~1049 ns/op -> ~78 ns/op
End-to-end record generate improves by ~20-34% on multi-key records.
🦋 Changeset detectedLatest commit: 92d476f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@fast-check/ava
fast-check
@fast-check/jest
@fast-check/packaged
@fast-check/poisoning
@fast-check/vitest
@fast-check/worker
commit: |
Contributor
⏱️ Benchmark ResultsClick to expand |
dubzzz
commented
Jun 4, 2026
…SeparateKeysToObject.ts
dubzzz
enabled auto-merge (squash)
June 4, 2026 21:48
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7046 +/- ##
=======================================
Coverage 94.86% 94.87%
=======================================
Files 212 212
Lines 5884 5889 +5
Branches 1543 1546 +3
=======================================
+ Hits 5582 5587 +5
Misses 294 294
Partials 8 8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This speeds up the
generatepath offc.record(...). Records are extremely common building blocks (directly, and insideletrec/memo/entityGraph/anything), so making their construction cheaper benefits a wide range of property-based tests. There is no observable behavior change: generated records have the exact same keys, values, property attributes (configurable/enumerable/writable) and prototype as before — this is a pure performance change with patch impact.What changed
The mapper that turns generated values into the final record object (
valuesAndSeparateKeysToObjectMapper) usedObject.definePropertyfor every present key:Object.definePropertywith all three flags set totrueproduces exactly the same own data property as a plain assignmentobj[key] = value, but in V8 it is markedly slower and tends to push the freshly created object onto a slower path. The hot loop now assigns directly, which keeps the object on the fast path and removes a per-property builtin call.The single exception is the string key
"__proto__": a plain assignment would trigger the prototype setter on a regular object instead of defining an own property, so that one key keeps usingdefineProperty. Symbol keys and every other string key (includingconstructor,toString, …) are plain data properties onObject.prototypeand are safely created by assignment, so they take the fast path.Why this design
__proto__guard, identical observable output.__proto__-only guard is provably sufficient —__proto__is the only accessor onObject.prototype; every other "dangerous" key (constructor,toString, …) is a plain data property that assignment safely shadows. The existing dangerous-key unit tests cover exactly this and still pass.Measurements
Isolated mapper microbenchmark (lower is better):
Object.definePropertyEnd-to-end
record(...).generate(...)improves by roughly 20–34% on multi-key records (the RNG dominates single-key records, where the mapper work is negligible). No regression observed on single-key records.Tests
No new tests were required: the behavior is unchanged and the existing suites already pin it down.
test/unit/arbitrary/_internals/mappers/ValuesAndSeparateKeysToObject.spec.ts(incl. the"__proto__"-as-key cases),record.spec.ts, andPartialRecordArbitraryBuilder.spec.tsall pass (47 tests), andtscis clean. A changeset (patch) is included.Checklist
— Don't delete this checklist and make sure you do the following before opening the PR
pnpm run bumpor by following the instructions from the changeset bot🐛(vitest) Something...) when the change targets a package other thanfast-checkGenerated by Claude Code