Performance & Production
View SourceThis guide covers the performance characteristics of ExJsonschema.
Performance Characteristics
ExJsonschema has two main operations:
- Schema Compilation: Takes milliseconds, should be done once
- Validation: Takes microseconds, scales with data size
Performance Profiles
The library provides three built-in performance profiles:
:performance- Fastest validation, minimal error information:lenient- Balanced speed and error detail:strict- Maximum error detail and format validation
perf_opts = ExJsonschema.Options.new(:performance)
ExJsonschema.validate(validator, data, perf_opts)Functions
validate/2- Returns detailed error informationvalid?/2- Returns boolean only, faster for simple checks
Caching
ExJsonschema automatically caches compiled schemas that have an $id field:
schema_with_id = ~s({
"$id": "http://myapp.com/schemas/user.json",
"type": "object",
"properties": {"name": {"type": "string"}}
})
# First call compiles and caches
{:ok, validator1} = ExJsonschema.compile(schema_with_id)
# Second call returns cached version
{:ok, validator2} = ExJsonschema.compile(schema_with_id)
# validator1 == validator2The default cache keeps schemas in memory indefinitely. You can implement your own cache by creating a module that implements the ExJsonschema.Cache behaviour:
# config/config.exs
config :ex_jsonschema, cache: MyApp.SchemaCacheFor tests, disable caching:
# config/test.exs
config :ex_jsonschema, cache: ExJsonschema.Cache.NoopBenchmarking
The library includes a benchmark tool:
mix benchmark
This measures validation performance with different profiles and data sizes.