Skip to content

Commit

Permalink
refactor(benchmarks): use fresh data object for each iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 9, 2023
1 parent 67b8e14 commit 19e5f0d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
24 changes: 12 additions & 12 deletions benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ node build/benchmarks/flat_object.js
### Results
```
===============================
Benchmarking with a flat object
Benchmarking with flat object
===============================
Vine x 9,208,428 ops/sec ±0.99% (86 runs sampled)
Zod x 1,158,997 ops/sec ±0.89% (87 runs sampled)
Yup x 413,173 ops/sec ±0.59% (90 runs sampled)
Vine x 9,149,087 ops/sec ±0.35% (89 runs sampled)
Zod x 1,167,525 ops/sec ±0.70% (85 runs sampled)
Yup x 404,760 ops/sec ±0.38% (86 runs sampled)
Fastest is Vine
```

Expand All @@ -38,9 +38,9 @@ node build/benchmarks/nested_object.js
=================================
Benchmarking with nested object
=================================
Vine x 8,157,981 ops/sec ±0.34% (89 runs sampled)
Zod x 558,226 ops/sec ±0.38% (87 runs sampled)
Yup x 192,073 ops/sec ±1.19% (90 runs sampled)
Vine x 8,024,818 ops/sec ±0.26% (89 runs sampled)
Zod x 553,710 ops/sec ±0.34% (88 runs sampled)
Yup x 191,407 ops/sec ±0.26% (88 runs sampled)
Fastest is Vine
```

Expand All @@ -57,9 +57,9 @@ node build/benchmarks/array.js
======================
Benchmarking arrays
======================
Vine x 6,816,681 ops/sec ±1.65% (90 runs sampled)
Zod x 420,825 ops/sec ±0.35% (91 runs sampled)
Yup x 119,343 ops/sec ±0.38% (91 runs sampled)
Vine x 6,564,289 ops/sec ±0.29% (88 runs sampled)
Zod x 410,636 ops/sec ±0.77% (87 runs sampled)
Yup x 114,580 ops/sec ±0.31% (88 runs sampled)
Fastest is Vine
```

Expand All @@ -78,7 +78,7 @@ node build/benchmarks/union.js
=======================
Benchmarking unions
=======================
Vine x 8,791,274 ops/sec ±0.65% (85 runs sampled)
Zod x 184,658 ops/sec ±0.53% (85 runs sampled)
Vine x 8,407,062 ops/sec ±0.28% (87 runs sampled)
Zod x 181,892 ops/sec ±0.34% (88 runs sampled)
Fastest is Vine
```
30 changes: 16 additions & 14 deletions benchmarks/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import { z } from 'zod'
import yup from 'yup'
import vine from '../index.js'

const data = {
contacts: [
{
type: 'email',
value: 'foo@bar.com',
},
{
type: 'phone',
value: '12345678',
},
],
function getData() {
return {
contacts: [
{
type: 'email',
value: 'foo@bar.com',
},
{
type: 'phone',
value: '12345678',
},
],
}
}

const zodSchema = z.object({
Expand Down Expand Up @@ -61,7 +63,7 @@ suite
.add('Vine', {
defer: true,
fn: function (deferred: any) {
vineSchema({ data })
vineSchema({ data: getData() })
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -70,7 +72,7 @@ suite
defer: true,
fn: function (deferred: any) {
zodSchema
.parseAsync(data)
.parseAsync(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -79,7 +81,7 @@ suite
defer: true,
fn: function (deferred: any) {
yupSchema
.validate(data)
.validate(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand Down
14 changes: 8 additions & 6 deletions benchmarks/flat_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { z } from 'zod'
import yup from 'yup'
import vine from '../index.js'

const data = {
username: 'virk',
password: 'secret',
function getData() {
return {
username: 'virk',
password: 'secret',
}
}

const zodSchema = z.object({
Expand Down Expand Up @@ -37,7 +39,7 @@ suite
.add('Vine', {
defer: true,
fn: function (deferred: any) {
vineSchema({ data })
vineSchema({ data: getData() })
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -46,7 +48,7 @@ suite
defer: true,
fn: function (deferred: any) {
zodSchema
.parseAsync(data)
.parseAsync(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -55,7 +57,7 @@ suite
defer: true,
fn: function (deferred: any) {
yupSchema
.validate(data)
.validate(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand Down
22 changes: 12 additions & 10 deletions benchmarks/nested_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { z } from 'zod'
import yup from 'yup'
import vine from '../index.js'

const data = {
username: 'virk',
password: 'secret',
contact: {
name: 'virk',
address: 'universe',
},
function getData() {
return {
username: 'virk',
password: 'secret',
contact: {
name: 'virk',
address: 'universe',
},
}
}

const zodSchema = z.object({
Expand Down Expand Up @@ -55,7 +57,7 @@ suite
.add('Vine', {
defer: true,
fn: function (deferred: any) {
vineSchema({ data })
vineSchema({ data: getData() })
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -64,7 +66,7 @@ suite
defer: true,
fn: function (deferred: any) {
zodSchema
.parseAsync(data)
.parseAsync(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -73,7 +75,7 @@ suite
defer: true,
fn: function (deferred: any) {
yupSchema
.validate(data)
.validate(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand Down
16 changes: 9 additions & 7 deletions benchmarks/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import vine from '../index.js'

const data = {
contact: {
type: 'phone',
mobile_number: '9210210102',
},
function getData() {
return {
contact: {
type: 'phone',
mobile_number: '9210210102',
},
}
}

const zodSchema = z.object({
Expand Down Expand Up @@ -53,7 +55,7 @@ suite
.add('Vine', {
defer: true,
fn: function (deferred: any) {
vineSchema({ data })
vineSchema({ data: getData() })
.then(() => deferred.resolve())
.catch(console.log)
},
Expand All @@ -62,7 +64,7 @@ suite
defer: true,
fn: function (deferred: any) {
zodSchema
.parseAsync(data)
.parseAsync(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
Expand Down

0 comments on commit 19e5f0d

Please sign in to comment.