Skip to content

Optimize Value.Convert property matching (up to 2.9x faster on object arrays) - #1740

Open
deadstrobe5 wants to merge 1 commit into
sinclairzx81:mainfrom
deadstrobe5:convert-property-set
Open

deadstrobe5 wants to merge 1 commit into
sinclairzx81:mainfrom
deadstrobe5:convert-property-set

Conversation

@deadstrobe5

Copy link
Copy Markdown
Contributor

Value.Convert matched object properties by building a RegExp (^key$) for each property on every call and testing it against every key of the value. This changes it to look each property key up in a Set of the value's own keys, the same way Value.Default already does. The additionalProperties branch still gets the same regex entries as before.

This also fixes keys with regex characters in them. Before, a property named $id was never converted (the $ ends the pattern), a.b also converted a sibling axb, and d( threw Invalid regular expression. I added a test for these in test/typebox/runtime/value/convert/object.ts.

Value.Convert Before After Speedup
1 object, 5 properties 3.1 us 1.7 us 1.8x
1 object, 20 properties 17.5 us 8.4 us 2.1x
1000 objects, 20 properties 17.7 ms 9.0 ms 2.0x
1000 objects, 50 properties 64.1 ms 22.0 ms 2.9x

Measured on Node 24 (Ryzen 5 5600X) with the old and new source loaded in the same process, 9 alternating rounds, medians. An empty object is unchanged. I also ran old and new side by side over 90,000 random schemas and values (nested objects, optional properties, unions, intersects, records, additionalProperties) and the output was identical. npm test passes.

This branch has not been deployed

No deployments
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.

1 participant