Skip to content

Conversation

@sberan
Copy link
Owner

@sberan sberan commented Jan 17, 2026

Introduce a React-like approach where handlers return code blocks
instead of imperatively mutating a CodeBuilder.

  • Add CodeBlock class with automatic indentation handling
  • Add code tagged template for composing blocks
  • Add helper functions: ifBlock, ifElseBlock, forBlock, compose
  • Create type-functional.ts as proof of concept

This enables cleaner, more testable code generation where each
handler is a pure function returning its generated code.

Introduce a React-like approach where handlers return code blocks
instead of imperatively mutating a CodeBuilder.

- Add CodeBlock class with automatic indentation handling
- Add `code` tagged template for composing blocks
- Add helper functions: ifBlock, ifElseBlock, forBlock, compose
- Create type-functional.ts as proof of concept

This enables cleaner, more testable code generation where each
handler is a pure function returning its generated code.
Remove helper functions (ifBlock, forBlock, etc.) - the `code`
template literal is sufficient for all patterns. Just write the
code you want to generate.

Also update type-functional.ts to use pure `code` templates
with inline if statements instead of helpers.
@github-actions
Copy link
Contributor

github-actions bot commented Jan 17, 2026

Benchmark Results

Only tests where both validators pass are compared (apples-to-apples)

tjs vs ajv

Draft Tests tjs ajv Winner
draft4 790 21.9M 13.5M 🟢 tjs 1.62×
draft6 1120 32.1M 14.9M 🟢 tjs 2.15×
draft7 1324 26.6M 13.3M 🟢 tjs 1.99×
draft2019-09 1703 25.1M 6.7M 🟢 tjs 3.76×
draft2020-12 1665 27.5M 6.8M 🟢 tjs 4.05×

Overall: 🟢 tjs is 2.94× faster on 6602 shared tests

tjs vs is-my-json-valid

Draft Tests tjs is-my-json-valid Winner
draft4 640 36.1M 15.6M 🟢 tjs 2.32×
draft6 703 41.5M 16.4M 🟢 tjs 2.52×
draft7 732 41.9M 17.9M 🟢 tjs 2.34×
draft2019-09 842 45.1M 19.2M 🟢 tjs 2.34×
draft2020-12 785 47.0M 19.7M 🟢 tjs 2.39×

Overall: 🟢 tjs is 2.38× faster on 3702 shared tests

tjs vs djv

Draft Tests tjs djv Winner
draft4 540 36.6M 3.5M 🟢 tjs 10.52×
draft6 706 43.7M 3.7M 🟢 tjs 11.95×
draft7 760 40.0M 4.0M 🟢 tjs 9.96×
draft2019-09 868 41.9M 4.6M 🟢 tjs 9.08×
draft2020-12 843 45.6M 4.9M 🟢 tjs 9.29×

Overall: 🟢 tjs is 10.07× faster on 3717 shared tests

tjs vs jsen

Draft Tests tjs jsen Winner
draft4 671 34.7M 15.1M 🟢 tjs 2.30×
draft6 707 39.5M 15.5M 🟢 tjs 2.55×
draft7 736 40.2M 17.2M 🟢 tjs 2.34×
draft2019-09 838 42.9M 18.5M 🟢 tjs 2.31×
draft2020-12 792 46.1M 18.1M 🟢 tjs 2.54×

Overall: 🟢 tjs is 2.40× faster on 3744 shared tests

tjs vs schemasafe

Draft Tests tjs schemasafe Winner
draft4 821 22.0M 21.7M 🟢 tjs 1.02×
draft6 1104 31.8M 23.4M 🟢 tjs 1.36×
draft7 1238 29.3M 21.0M 🟢 tjs 1.40×
draft2019-09 1641 28.1M 18.9M 🟢 tjs 1.48×
draft2020-12 1540 27.4M 15.1M 🟢 tjs 1.82×

Overall: 🟢 tjs is 1.45× faster on 6344 shared tests

Instead of manually writing if/error blocks:
  code`if (!(${check})) { ${genError(...)} }`

Use the fail.unless primitive:
  fail.unless(check, ctx, 'keyword', 'message', params)

Takes the PASSING condition and negates internally,
avoiding operator precedence bugs with manual negation.
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