Schema validation performance versus Ajv

未关闭
#10,772 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
活跃
技术栈
typescript
领域
performance

调研方向

Start by locating the Effect Schema interpreter, issue collection and generated fast paths, then compare their behavior with the reproducible benchmarks for structs containing a Json index-signature child or a Uint8Array field. Measure valid and error paths and check the existing required, enum, pattern, length and onExcessProperty options. Done means a measured improvement with generated results matching interpreter behavior and validation semantics preserved.

由索引模型根据 Issue 内容生成。

描述

Background

I use Effect Schema to validate data at storage and API boundaries. Ajv is the standard, well-maintained JSON Schema validator and the natural point of comparison. I benchmarked the two because a performance gap in this layer would affect a lot of applications, even if most of them would never notice it in day-to-day use. Effect targets size and speed, so I think a gap of this size is worth understanding and reducing where that can be done without changing what validation means.

What I measured

I started with a representative set of record-like schemas, which I have not included here. Against Ajv, Effect was roughly 4.7x slower on the valid path and roughly 57x slower on the error path. The synthetic shapes I use for the reproducible benchmark are simpler, so they show smaller gaps: about 2.7x on a boolean error path and about 2.8x and 1.8x from generated fast paths. Those two shapes are a struct with a Json index-signature child and a struct with a Uint8Array field. The synthetic results do not reproduce the larger ratios from the representative set, and nothing in this report depends on that set being available.

The mechanism is consistent across both measurements. Valid documents spend their time in the interpreter and in issue collection, even when no issue is produced. Errors spend time in error-mode handling and in the per-key excess-property check. Thrown SchemaError objects are not the main cost: throwing, returning and bare-issue paths all landed within 13% of each other.

Possible directions

These are candidates I want to discuss, not commitments I am making here:

  • An options-aware boolean validation path. Callers that only need accept or reject could enforce a strict contract such as onExcessProperty: "error" without building issue details on the valid path.
  • Generated fast paths for common declaration shapes. The Json index signature and Uint8Array shapes currently push a whole struct onto the interpreter; compiling them directly is a contained way to bring more schemas onto the fast path.
  • Caching excess-key metadata. Declared property names are fixed per schema, so rebuilding them for every document looks avoidable.

Scope and limits

I do not want to change validation semantics. Any direction has to keep the existing checks and options, including required, enum, pattern, length and onExcessProperty, and generated results need to match the interpreter. The numbers come from one host and are relative signals rather than a service level guarantee.

主要语言
TypeScript
星标
16.7k
派生
808
平均合并
21 小时 22 分钟
30 天内合并 PR
486

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Effect-TS/effect 的其他 Issue

查看 Effect-TS/effect 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。