Skip to content

perf: read each additional property once - #930

Merged
Tony133 merged 2 commits into
fastify:mainfrom
iammerlijn:perf/additional-props
Nov 20, 2026
Merged

Tony133 merged 2 commits into
fastify:mainfrom
iammerlijn:perf/additional-props

Conversation

@iammerlijn

Copy link
Copy Markdown
Contributor

This makes objects with additionalProperties or patternProperties 16-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) reads obj[key] up to 4 times: 3 times in the skip test (undefined, function, symbol) and once more for const value. These are dynamic keyed loads, so V8 can't make them cheap the way it does for obj["name"].

Before #817 the loop used Object.entries and read each value once. #817 switched to Object.keys to 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 reads const value = obj[key] once and tests value. The declared-key test is left out when there are no declared properties.
  • benchmark/bench.js: three new cases (additionalProperties schema, additionalProperties: true, patternProperties). The bench had no case for this loop before, only additionalProperties: false, which does not use it.

One thing changes: a getter or a Proxy get trap 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 as JSON.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:

case Node 24.22 main → PR Node 26.13 main → PR
object with additionalProperties (id + 9 string keys) 1.69M → 2.09M (+23.2%) 1.51M → 2.01M (+32.8%)
object with additionalProperties: true (10 mixed values) 1.13M → 1.31M (+15.8%) 1.25M → 1.55M (+23.8%)
object with patternProperties (10 keys) 1.13M → 1.34M (+19.2%) 1.09M → 1.37M (+25.7%)

Through build(schema)(input), median of 11 runs, separate processes:

case Node 24.22 main → PR Node 26.13 main → PR
dictionary { additionalProperties: { type: 'string' } }, 30 keys 367k → 464k (+26.5%) 346k → 447k (+29.3%)
list of 20 users, each with a 5-key metadata map 64.0k → 70.6k (+10.3%) 62.2k → 69.1k (+11.1%)

You can check it with npm run bench:cmp against main.

Tests

  • New tests in test/additionalProperties.test.js:
    • undefined, function and symbol values are skipped, and integer keys keep their order, for an additionalProperties schema, a dictionary, additionalProperties: true, patternProperties with an additionalProperties fallback, and additionalProperties: false
    • a getter on an extra property is read once, for additionalProperties and patternProperties
  • The first test also passes on main. The getter test fails on main.
  • npm test passes with 100% coverage, and lint is clean.

Related: #817 (switched the loop to Object.keys), #707 and #759 (cached obj[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

iammerlijn and others added 2 commits November 20, 2026 09:48
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.

@Tony133 Tony133 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

@Tony133
Tony133 merged commit b3261b7 into fastify:main Nov 20, 2026
64 of 70 checks passed
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