logo
When using schemas, Fastify's startup speed is heavily affected by its schema compiler, AJV. AJV compiles every schema on startup. That's not the problem. The problem is it's quite slow, and they admit this themselves on their docs: https://ajv.js.org/guide/getting-started.html The more routes, the more complex your schemas, the longer you wait. For most people this isn't a bottleneck — schemas are relatively simple and routes are few. But for serverless functions, situations where every ms counts, or where you need to compile schemas on demand, it matters. Swapping the schema compiler fixes it. 100 routes, complex schemas (allOf/oneOf/anyOf/conditionals): JetValidator has removeAdditional, useDefaults and coerceTypes enabled. AJV has none, while fastify's default also has those 3 options enabled internally

Fastify default AJV: 658ms AJV no options: 425ms @jetio/fastify-validator: 122ms

100 routes, 500 properties per schema: Same options setup.

Fastify default AJV: 69s AJV no options: 47s @jetio/fastify-validator: 2.6s

JetValidator with full options enabled. AJV with none. Still 18x faster. Benchmarks are reproducible, Run them yourself: https://github.com/official-jetio/fastify-validator

It's a custom schemaController or buildValidator for Fastify that lets you swap the compiler it uses. Two functions to drop it in: npm i @jetio/fastify-validator

const fastify = Fastify({ schemaController: createJetValidatorController({ removeAdditional: true, useDefaults: true, coerceTypes: true, }), });

Or using buildValidator directly if you need more control:

const fastify = Fastify({ schemaController: { compilersFactory: { buildValidator: createJetValidatorCompiler({ removeAdditional: true, useDefaults: true, coerceTypes: true, }), }, }, });

This was built specifically for developers who have issues with Fastify startup time or want faster startups. If you don't then you don't need this.

The package is powered by @jetio/validator, a full AJV replacement built from scratch. 19x faster compilation on average across 62 schemas. 72% overall win rate in validation. 99.5% JSON Schema compliant across drafts 06 through 2020-12. Zero dependencies. 26KB gzipped. Everything AJV does is built in, formats, custom keywords, $data, $ref, async validation and custom elseIf keyword no extra packages needed.

Fastify validator: https://github.com/official-jetio/fastify-validator Schema Builder with json schema compliant type inference: https://github.com/official-jetio/schema-builder Validator: https://github.com/official-jetio/validator Benchmarks: https://github.com/official-jetio/validator#benchmarks Docs: https://github.com/official-jetio/validator/blob/main/DOCUME...