Skip to content

Commit

Permalink
chore: add builders benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Aug 6, 2024
1 parent 1efa30f commit 15c18ba
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ always kept up-to-date.

> **Note:** The API is the same across all JavaScript runtimes, regardless of [installation](#install) source. Only the package's name changes.
## Benchmarks

> Run on Deno 1.45.5 `(release, aarch64-apple-darwin)` with v8 `12.7.224.13`
```
file: tschema/scripts/bench.ts
runtime: deno 1.45.5 (aarch64-apple-darwin)
# Builders
tschema 5,273,942.5 iter/sec 189.61 ns/iter
sinclair/typebox 130,548.3 iter/sec 7.66 µs/iter
# Summary
tschema
40.4x faster than sinclair/typebox
```

## License

MIT © [Luke Edwards](https://lukeed.com)
71 changes: 71 additions & 0 deletions scripts/bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as t from '../mod.ts';
import { Type } from 'npm:@sinclair/typebox';

Deno.bench('tschema', { group: 'builder' }, () => {
let _ = t.object({
uid: t.integer(),
name: t.string({
description: 'full name',
examples: ['Alex Johnson'],
}),
isActive: t.boolean(),
avatar: t.optional(
t.string({ format: 'uri' }),
),
achievements: t.tuple([
t.number({ minimum: 0 }), // points
t.enum(['novice', 'pro', 'expert', 'master']),
]),
interests: t.array(
t.string({
minLength: 4,
maxLength: 36,
}),
),
last_updated: t.integer({
minimum: 0,
examples: [1722642982],
description: 'unix seconds',
}),
});
});

Deno.bench('sinclair/typebox', { group: 'builder' }, () => {
let _ = Type.Object({
uid: Type.Integer(),
name: Type.String({
description: 'full name',
examples: ['Alex Johnson'],
}),
isActive: Type.Boolean(),
avatar: Type.Optional(
Type.String({ format: 'uri' }),
),
achievements: Type.Tuple([
Type.Number({ minimum: 0 }), // points
// NOTE: different
// Type.Enum({
// NOVICE: 'novice',
// PRO: 'pro',
// EXPERT: 'expert',
// MASTER: 'master',
// }),

// NOTE: equivalent output
Type.Unsafe<string>({
enum: ['novice', 'pro', 'expert', 'master'],
}),
]),
interests: Type.Array(
Type.String({
minLength: 4,
maxLength: 36,
}),
),
last_updated: Type.Integer({
minimum: 0,
examples: [1722642982],
description: 'unix seconds',
}),
});
});

0 comments on commit 15c18ba

Please sign in to comment.