Repository navigation
perf: read each additional property once - #930
Merged
Merged
Conversation
The generated loop for additionalProperties and patternProperties read obj[key] up to 4 times per extra key: 3 times in the skip test (undefined, function, symbol) and once for the value. Skip declared keys first, then read the value once and test it. A getter or Proxy get trap on an extra property now runs once, the same as JSON.stringify.
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.
This makes objects with
additionalPropertiesorpatternProperties16-33% faster to serialize. A list response where each item has a small map is 10-11% faster. The output does not change for plain objects.Why
For every key that is not a declared property, the generated loop in
buildExtraObjectPropertiesSerializer(index.js:713-721) readsobj[key]up to 4 times: 3 times in the skip test (undefined,function,symbol) and once more forconst value. These are dynamic keyed loads, so V8 can't make them cheap the way it does forobj["name"].Before #817 the loop used
Object.entriesand read each value once. #817 switched toObject.keysto skip the read for declared keys, but the 3 extra reads ended up in the condition. This PR keeps the goal of #817 (declared keys are still not read) and goes back to one read per key.What changed
index.js: the generated loop skips declared keys first, then readsconst value = obj[key]once and testsvalue. The declared-key test is left out when there are no declared properties.benchmark/bench.js: three new cases (additionalPropertiesschema,additionalProperties: true,patternProperties). The bench had no case for this loop before, onlyadditionalProperties: false, which does not use it.One thing changes: a getter or a Proxy
gettrap on an extra property now runs once instead of up to 4 times. A getter that returns different values on each read now gives the first value, the same asJSON.stringify. Before, the key could be dropped or a later value could be written. The "never reread getters" rule in the codegen (index.js:1188) already asks for this. I added a test for it.Numbers
Apple M3 Pro, the repo's bench runner, median ops/s of 5 runs, old and new in turn:
Through
build(schema)(input), median of 11 runs, separate processes:{ additionalProperties: { type: 'string' } }, 30 keysmetadatamapYou can check it with
npm run bench:cmpagainstmain.Tests
test/additionalProperties.test.js:additionalPropertiesschema, a dictionary,additionalProperties: true,patternPropertieswith anadditionalPropertiesfallback, andadditionalProperties: falseadditionalPropertiesandpatternPropertiesmain. The getter test fails onmain.npm testpasses with 100% coverage, and lint is clean.Related: #817 (switched the loop to
Object.keys), #707 and #759 (cachedobj[key]for declared properties and arrays).I used Claude (Opus 5.5) to write and benchmark this change. I checked the results and the reasoning before opening it.
Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct