⚡️ Faster fc.dictionary on generate#7047
Merged
Merged
Conversation
Build generated dictionaries with direct property assignment instead of
Object.defineProperty in the key-value-pairs-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/number keys and every other string key are assigned directly.
Isolated mapper microbenchmark (lower is better):
- 3 keys: ~799 ns/op -> ~59 ns/op
- 10 keys: ~2518 ns/op -> ~188 ns/op
🦋 Changeset detectedLatest commit: d2429e9 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: |
dubzzz
commented
Jun 4, 2026
dubzzz
enabled auto-merge (squash)
June 4, 2026 21:53
Contributor
⏱️ Benchmark ResultsClick to expand |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7047 +/- ##
=======================================
Coverage 94.86% 94.87%
=======================================
Files 212 212
Lines 5884 5889 +5
Branches 1543 1545 +2
=======================================
+ 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.dictionary(...). There is no observable behavior change: generated dictionaries have the exact same keys, values, property attributes (configurable/enumerable/writable) and prototype (object vs null) as before — a pure performance change with patch impact.What changed
The mapper that turns generated
[key, value]pairs into the final object (keyValuePairsToObjectMapper) usedObject.definePropertyfor every pair: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.As with the matching
fc.recordchange, 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/number keys and every other string key (includingconstructor,toString, …) are plain data properties and are safely created by assignment.Why this design
__proto__-only guard is provably sufficient —__proto__is the only accessor onObject.prototype; every other "dangerous" key is a plain data property that assignment safely shadows. The existing dangerous-key unit tests (['__proto__', 'constructor', 'toString'], both object- and null-prototype) cover exactly this and still pass.Measurements
Isolated mapper microbenchmark (lower is better):
Object.definePropertyfc.dictionary().generate()is dominated by string-key generation, so the end-to-end gain is most visible on larger dictionaries; the mapper itself is ~13× faster.Tests
No new tests required — behavior is unchanged and the existing suites pin it down.
test/unit/arbitrary/_internals/mappers/KeyValuePairsToObject.spec.ts(incl. the dangerous-key and null-prototype cases) anddictionary.spec.tsall pass (24 tests);tscis clean. A changeset (patch) is included.This is a sibling of the
fc.recordmapper change (kept as a separate, single-concern PR).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