ata-validator 0.4.13 → 0.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjson/simdjson). Multi-core parallel validation, RE2 regex, codegen bytecode engine. Standard Schema V1 compatible.
4
4
 
5
- **[ata-validator.com](https://ata-validator.com)**
5
+ **[ata-validator.com](https://ata-validator.com)** | **[API Docs](docs/API.md)** | **[Contributing](CONTRIBUTING.md)**
6
6
 
7
7
  ## Performance
8
8
 
@@ -10,13 +10,24 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
10
10
 
11
11
  | Scenario | ata | ajv | |
12
12
  |---|---|---|---|
13
- | **validate(obj)** valid | 25.5M ops/sec | 19.3M ops/sec | **ata 1.3x faster** |
14
- | **validate(obj)** invalid | 17.7M ops/sec | 13.5M ops/sec | **ata 1.3x faster** |
15
- | **isValidObject(obj)** | 39.5M ops/sec | 17.6M ops/sec | **ata 2.2x faster** |
16
- | **Constructor cold start** | 1.28M ops/sec | 812 ops/sec | **ata 1,580x faster** |
17
- | **First validation** | 396K ops/sec | 880 ops/sec | **ata 450x faster** |
13
+ | **validate(obj)** valid | 16.6M ops/sec | 9.8M ops/sec | **ata 1.7x faster** |
14
+ | **validate(obj)** invalid | 10.0M ops/sec | 5.6M ops/sec | **ata 1.8x faster** |
15
+ | **isValidObject(obj)** | 36.0M ops/sec | 9.4M ops/sec | **ata 3.8x faster** |
16
+ | **Schema compilation** | 1.6M ops/sec | 781 ops/sec | **ata 2,067x faster** |
17
+ | **First validation** | 614K ops/sec | 775 ops/sec | **ata 793x faster** |
18
18
 
19
- > validate(obj) numbers are isolated single-schema benchmarks. Multi-schema benchmark overhead reduces throughput; real-world numbers depend on workload.
19
+ > Measured with [mitata](https://github.com/evanwashere/mitata) (process-isolated). Results vary by workload and hardware.
20
+
21
+ ### vs typebox 1.x (with format: 'email')
22
+
23
+ | Scenario | ata | typebox | |
24
+ |---|---|---|---|
25
+ | **isValidObject(obj)** valid | 90.9M ops/sec | 18.2M ops/sec | **ata 5.0x faster** |
26
+ | **isValidObject(obj)** invalid | 435M ops/sec | 169M ops/sec | **ata 2.6x faster** |
27
+ | **Schema compilation** | 2.0M ops/sec | 18.5K ops/sec | **ata 110x faster** |
28
+ | **First validation** | 1.55M ops/sec | 18.2K ops/sec | **ata 85x faster** |
29
+
30
+ > typebox 1.x is JSON Schema compliant with RFC format support. [Benchmark code](benchmark/bench_vs_typebox_esm.mjs)
20
31
 
21
32
  ### Large Data - JS Object Validation
22
33
 
@@ -53,8 +64,8 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
53
64
 
54
65
  ## When to use ata
55
66
 
56
- - **High-throughput `validate(obj)`** - 25.5M ops/sec valid, 17.7M ops/sec invalid
57
- - **Serverless / cold starts** - 1,580x faster constructor, 450x faster first validation
67
+ - **High-throughput `validate(obj)`** - 16.6M ops/sec valid, 10.0M ops/sec invalid
68
+ - **Serverless / cold starts** - 2,067x faster compilation, 793x faster first validation
58
69
  - **Security-sensitive apps** - RE2 regex, immune to ReDoS attacks
59
70
  - **Batch/streaming validation** - NDJSON log processing, data pipelines (2.6x faster)
60
71
  - **Standard Schema V1** - native support for Fastify v5, tRPC, TanStack
@@ -67,7 +78,7 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
67
78
 
68
79
  ## Features
69
80
 
70
- - **Hybrid validator**: 25.5M ops/sec valid, 17.7M ops/sec invalid - codegen + single-pass error collection. No try/catch, no double pass. Schema compilation cache for repeated schemas
81
+ - **Hybrid validator**: 16.6M ops/sec valid, 10.0M ops/sec invalid - codegen + single-pass error collection. No try/catch, no double pass. Schema compilation cache for repeated schemas
71
82
  - **Multi-core**: Parallel validation across all CPU cores - 13.4M validations/sec
72
83
  - **simdjson**: SIMD-accelerated JSON parsing at GB/s speeds, adaptive On Demand for large docs
73
84
  - **RE2 regex**: Linear-time guarantees, immune to ReDoS attacks (2391x faster on pathological input)
@@ -234,6 +245,7 @@ npm run test:suite
234
245
 
235
246
  MIT
236
247
 
237
- ## Author
248
+ ## Authors
238
249
 
239
250
  [Mert Can Altin](https://github.com/mertcanaltin)
251
+ [Daniel Lemire](https://github.com/lemire)
@@ -523,6 +523,11 @@ function compileToJSCodegen(schema) {
523
523
  genCode(schema, 'd', lines, ctx)
524
524
  if (lines.length === 0) return () => true
525
525
 
526
+ // Append deferred checks (additionalProperties) at the end
527
+ if (ctx.deferredChecks) {
528
+ for (const dc of ctx.deferredChecks) lines.push(dc)
529
+ }
530
+
526
531
  const checkStr = lines.join('\n ')
527
532
 
528
533
  // Regex and helpers are passed as closure variables (not re-created per call)
@@ -634,7 +639,7 @@ function genCode(schema, v, lines, ctx, knownType) {
634
639
  case 'string': return `typeof ${v}==='string'`
635
640
  case 'number': return `(typeof ${v}==='number'&&isFinite(${v}))`
636
641
  case 'integer': return `Number.isInteger(${v})`
637
- case 'boolean': return `typeof ${v}==='boolean'`
642
+ case 'boolean': return `(${v}===true||${v}===false)`
638
643
  case 'null': return `${v}===null`
639
644
  default: return 'true'
640
645
  }
@@ -676,13 +681,18 @@ function genCode(schema, v, lines, ctx, knownType) {
676
681
  // Collect required keys so property checks can skip 'in' guard
677
682
  const requiredSet = new Set(schema.required || [])
678
683
 
679
- // required: direct property access (faster than destructuring in V8)
680
- const hoisted = {} // key -> access expression (no local vars)
684
+ // required: skip explicit check if property has a type constraint
685
+ // (type check on undefined returns false anyway: Number.isInteger(undefined) === false)
686
+ const hoisted = {} // key -> access expression
681
687
  if (schema.required && schema.properties && isObj) {
682
688
  const reqChecks = []
683
689
  for (const key of schema.required) {
684
690
  hoisted[key] = `${v}[${JSON.stringify(key)}]`
685
- reqChecks.push(`${v}[${JSON.stringify(key)}]===undefined`)
691
+ const prop = schema.properties[key]
692
+ const hasTypeCheck = prop && (prop.type || prop.enum || prop.const !== undefined)
693
+ if (!hasTypeCheck) {
694
+ reqChecks.push(`${v}[${JSON.stringify(key)}]===undefined`)
695
+ }
686
696
  }
687
697
  if (reqChecks.length > 0) {
688
698
  lines.push(`if(${reqChecks.join('||')})return false`)
@@ -745,18 +755,20 @@ function genCode(schema, v, lines, ctx, knownType) {
745
755
  lines.push(isArr ? `{${inner}}` : `if(Array.isArray(${v})){${inner}}`)
746
756
  }
747
757
 
748
- // additionalProperties
758
+ // additionalProperties -- deferred to end of function for better V8 optimization
759
+ // (type checks run first in hot path, expensive prop count check last)
749
760
  if (schema.additionalProperties === false && schema.properties) {
750
761
  const propCount = Object.keys(schema.properties).length
751
762
  if (!schema.patternProperties) {
752
- // Fast path: known property count, just check length
753
- const inner = `if(Object.getOwnPropertyNames(${v}).length!==${propCount})return false`
754
- lines.push(isObj ? inner : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
763
+ const inner = `var _n=0;for(var _k in ${v})_n++;if(_n!==${propCount})return false`
764
+ if (!ctx.deferredChecks) ctx.deferredChecks = []
765
+ ctx.deferredChecks.push(isObj ? inner : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
755
766
  } else {
756
767
  const allowed = Object.keys(schema.properties).map(k => `'${esc(k)}'`).join(',')
757
768
  const ci = ctx.varCounter++
758
769
  const inner = `const _k${ci}=Object.keys(${v});const _a${ci}=new Set([${allowed}]);for(let _i=0;_i<_k${ci}.length;_i++)if(!_a${ci}.has(_k${ci}[_i]))return false`
759
- lines.push(isObj ? `{${inner}}` : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
770
+ if (!ctx.deferredChecks) ctx.deferredChecks = []
771
+ ctx.deferredChecks.push(isObj ? `{${inner}}` : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
760
772
  }
761
773
  }
762
774
 
@@ -987,7 +999,7 @@ function genCodeE(schema, v, pathExpr, lines, ctx) {
987
999
  case 'string': return `typeof ${v}==='string'`
988
1000
  case 'number': return `(typeof ${v}==='number'&&isFinite(${v}))`
989
1001
  case 'integer': return `Number.isInteger(${v})`
990
- case 'boolean': return `typeof ${v}==='boolean'`
1002
+ case 'boolean': return `(${v}===true||${v}===false)`
991
1003
  case 'null': return `${v}===null`
992
1004
  default: return 'true'
993
1005
  }
@@ -1314,7 +1326,7 @@ function genCodeC(schema, v, pathExpr, lines, ctx) {
1314
1326
  case 'string': return `typeof ${v}==='string'`
1315
1327
  case 'number': return `(typeof ${v}==='number'&&isFinite(${v}))`
1316
1328
  case 'integer': return `Number.isInteger(${v})`
1317
- case 'boolean': return `typeof ${v}==='boolean'`
1329
+ case 'boolean': return `(${v}===true||${v}===false)`
1318
1330
  case 'null': return `${v}===null`
1319
1331
  default: return 'true'
1320
1332
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ata-validator",
3
- "version": "0.4.13",
3
+ "version": "0.4.15",
4
4
  "description": "Ultra-fast JSON Schema validator. Beats ajv on every valid-path benchmark: 1.1x–2.7x faster validate(obj), 151x faster compilation, 5.9x faster parallel batch. Speculative validation with V8-optimized JS codegen, simdjson, multi-core. Standard Schema V1 compatible.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -64,8 +64,10 @@
64
64
  },
65
65
  "devDependencies": {
66
66
  "@sinclair/typebox": "^0.34.49",
67
+ "mitata": "^1.0.34",
67
68
  "node-gyp": "^11.0.0",
68
- "prebuildify": "^6.0.1"
69
+ "prebuildify": "^6.0.1",
70
+ "typebox": "^1.1.7"
69
71
  },
70
72
  "gypfile": true
71
73
  }