Skip to content

Commit

Permalink
bench: add yup and fastest-validators to the benchmarks (#463)
Browse files Browse the repository at this point in the history
Should've found fastest-validator earlier, since it does some stuff the same way as the generated validators, but we are good chip slower than them.
  • Loading branch information
dirkdev98 authored Nov 3, 2020
1 parent f272da7 commit 73a22eb
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"devDependencies": {
"@types/node": "14.14.6",
"axios": "0.21.0",
"fastest-validator": "1.8.0",
"form-data": "3.0.0",
"lerna": "3.22.1",
"react": "16.14.0",
"react-query": "2.25.2",
"typescript": "4.0.5"
"typescript": "4.0.5",
"yup": "0.29.3"
},
"maintainers": [
{
Expand Down
200 changes: 195 additions & 5 deletions packages/code-gen/test/validators.bench.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,104 @@
import { bench, mainBenchFn } from "@lbu/cli";
import FastestValidator from "fastest-validator";
import * as yup from "yup";

mainBenchFn(import.meta);

bench("object validator simple", async (b) => {
// Expected output when logging all options
// lbuSimple: [Object: null prototype] { foo: true, bar: 5, baz: 'ok' }
// lbuNested: [Object: null prototype] {
// foo: true,
// bar: 5,
// nest: [
// 'foo',
// [Object: null prototype] { foo: true, bar: 15, baz: 'yes' },
// [Object: null prototype] { foo: true, bar: 15, baz: 'yes' },
// 'bar'
// ]
// }

// fvSimple: true
// fvNested: true

// yupSimple: { baz: 'ok', bar: 5, foo: true }
// yupNested: {
// foo: true,
// bar: 5,
// nest: [
// 'foo',
// { foo: true, bar: 15, baz: 'Yes ' },
// { foo: true, bar: 15, baz: 'Yes ' },
// 'bar'
// ]
// }
// }

const yupSimple = yup.object().shape({
foo: yup.bool().required(),
bar: yup.number().required().integer(),
baz: yup.string().required().trim().lowercase(),
});

// A bit convoluted, since Yup doesn't allow for nested schema's in oneOf
// https://github.com/jquense/yup/issues/662
const yupNested = yup.object().shape({
foo: yup.mixed().required().oneOf([true]),
bar: yup.mixed().required().oneOf([5]),
nest: yup
.array()
.required()
.of(
yup.mixed().when({
is: (value) => typeof value === "string",
then: yup.mixed().required().oneOf(["foo", yupSimple, "bar"]),
otherWise: yupSimple,
}),
),
});

const fastestValidator = new FastestValidator({});

const simpleFastestValidatorReusable = {
foo: { type: "boolean" },
bar: { type: "number", integer: true, convert: true },
baz: { type: "string", trim: true, lowercase: true },
$$strict: true,
};
const fastestValidatorSimple = fastestValidator.compile(
simpleFastestValidatorReusable,
);

const fastestValidatorNested = fastestValidator.compile({
foo: {
type: "equal",
value: true,
strict: true,
},
bar: {
type: "equal",
value: 5,
strict: true,
},
nest: {
type: "array",
items: [
{
type: "equal",
value: "foo",
strict: true,
},
{ type: "object", ...simpleFastestValidatorReusable },
{
type: "equal",
value: "bar",
strict: true,
},
],
},
$$strict: true,
});

bench("lbu validator simple", async (b) => {
const { validateBenchSimple } = await import(
"../../../generated/testing/bench/index.js"
);
Expand All @@ -12,14 +108,48 @@ bench("object validator simple", async (b) => {
for (let i = 0; i < b.N; ++i) {
// eslint-disable-next-line no-unused-vars
y = validateBenchSimple({
foo: "true",
bar: "5",
baz: "Ok",
foo: true,
bar: 5,
baz: "Ok ",
});
}
});

bench("object validator nested", async (b) => {
bench("yup validator simple", (b) => {
let y;
for (let i = 0; i < b.N; ++i) {
// eslint-disable-next-line no-unused-vars
y = yupSimple.validateSync(
{
foo: true,
bar: 5,
baz: "Ok ",
},
{
stripUnknown: true,
},
);
}
});

bench("fastest-validator validator simple", (b) => {
let y;
for (let i = 0; i < b.N; ++i) {
// eslint-disable-next-line no-unused-vars
y = fastestValidatorSimple(
{
foo: true,
bar: 5,
baz: "Ok ",
},
{
stripUnknown: true,
},
);
}
});

bench("lbu validator nested", async (b) => {
const { validateBenchNested } = await import(
"../../../generated/testing/bench/index.js"
);
Expand Down Expand Up @@ -48,3 +178,63 @@ bench("object validator nested", async (b) => {
});
}
});

bench("yup validator nested", (b) => {
let y;
for (let i = 0; i < b.N; ++i) {
// eslint-disable-next-line no-unused-vars
y = yupNested.validateSync(
{
foo: true,
bar: 5,
nest: [
"foo",
{
foo: true,
bar: 15,
baz: "Yes ",
},
{
foo: true,
bar: 15,
baz: "Yes ",
},
"bar",
],
},
{
stripUnknown: true,
},
);
}
});

bench("fastest-validator validator nested", (b) => {
let y;
for (let i = 0; i < b.N; ++i) {
// eslint-disable-next-line no-unused-vars
y = fastestValidatorNested(
{
foo: true,
bar: 5,
nest: [
"foo",
{
foo: true,
bar: 15,
baz: "Yes ",
},
{
foo: true,
bar: 15,
baz: "Yes ",
},
"bar",
],
},
{
stripUnknown: true,
},
);
}
});
50 changes: 50 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.11.5.tgz#c7ff6303df71080ec7a4f5b8c003c58f1cf51037"
integrity sha512-X9rD8qqm695vgmeaQ4fvz/o3+Wk4ZzQvSHkDBgpYKxpD4qTAUm88ZKtHkVqIOsYFFbIQ6wQYhC6q7pjqVK0E0Q==

"@babel/runtime@^7.10.5":
version "7.12.1"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.1.tgz#b4116a6b6711d010b2dad3b7b6e43bf1b9954740"
integrity sha512-J5AIf3vPj3UwXaAzb5j1xM4WAQDX3EMgemF8rjCP3SoW09LfRKAXQKt6CoVYl230P6iWdRcBbnLDDdnqWxZSCA==
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.5.5":
version "7.11.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
Expand Down Expand Up @@ -2674,6 +2681,11 @@ fast-levenshtein@^2.0.6:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=

[email protected]:
version "1.8.0"
resolved "https://registry.yarnpkg.com/fastest-validator/-/fastest-validator-1.8.0.tgz#e2f15c87d88fae67f51a4b078eccb3aaae218ea5"
integrity sha512-/5p7l7jmKtYhDhoxoineR7xW+BcZ5gZ4wTkp5lEyI7ESsqt6j5QJazxPmFNUl+hKOsATvw8+5yx0yt+V9MJR6w==

figgy-pudding@^3.4.1, figgy-pudding@^3.5.1:
version "3.5.2"
resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.2.tgz#b4eee8148abb01dcf1d1ac34367d59e12fa61d6e"
Expand Down Expand Up @@ -2770,6 +2782,11 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

fn-name@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/fn-name/-/fn-name-3.0.0.tgz#0596707f635929634d791f452309ab41558e3c5c"
integrity sha512-eNMNr5exLoavuAMhIUVsOKF79SWd/zG104ef6sxBTSw+cZc6BXdQXDvYcGvp0VbxVVSp1XDUNoz7mg1xMtSznA==

follow-redirects@^1.0.0, follow-redirects@^1.10.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db"
Expand Down Expand Up @@ -4006,6 +4023,11 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash-es@^4.17.11:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -5079,6 +5101,11 @@ prop-types@^15.6.2:
object-assign "^4.1.1"
react-is "^16.8.1"

property-expr@^2.0.2:
version "2.0.4"
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.4.tgz#37b925478e58965031bb612ec5b3260f8241e910"
integrity sha512-sFPkHQjVKheDNnPvotjQmm3KD3uk1fWKUN7CrpdbwmUx3CrG3QiM8QpTSimvig5vTXmTvjz7+TDvXOI9+4rkcg==

proto-list@~1.2.1:
version "1.2.4"
resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
Expand Down Expand Up @@ -5989,6 +6016,11 @@ supports-color@^7.1.0:
dependencies:
has-flag "^4.0.0"

synchronous-promise@^2.0.13:
version "2.0.15"
resolved "https://registry.yarnpkg.com/synchronous-promise/-/synchronous-promise-2.0.15.tgz#07ca1822b9de0001f5ff73595f3d08c4f720eb8e"
integrity sha512-k8uzYIkIVwmT+TcglpdN50pS2y1BDcUnBPK9iJeGu0Pl1lOI8pD6wtzgw91Pjpe+RxtTncw32tLxs/R0yNL2Mg==

table@^5.2.3:
version "5.4.6"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
Expand Down Expand Up @@ -6132,6 +6164,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

toposort@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330"
integrity sha1-riF2gXXRVZ1IvvNUILL0li8JwzA=

tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
Expand Down Expand Up @@ -6628,3 +6665,16 @@ ylru@^1.2.0:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ylru/-/ylru-1.2.1.tgz#f576b63341547989c1de7ba288760923b27fe84f"
integrity sha512-faQrqNMzcPCHGVC2aaOINk13K+aaBDUPjGWl0teOXywElLjyVAB6Oe2jj62jHYtwsU49jXhScYbvPENK+6zAvQ==

[email protected]:
version "0.29.3"
resolved "https://registry.yarnpkg.com/yup/-/yup-0.29.3.tgz#69a30fd3f1c19f5d9e31b1cf1c2b851ce8045fea"
integrity sha512-RNUGiZ/sQ37CkhzKFoedkeMfJM0vNQyaz+wRZJzxdKE7VfDeVKH8bb4rr7XhRLbHJz5hSjoDNwMEIaKhuMZ8gQ==
dependencies:
"@babel/runtime" "^7.10.5"
fn-name "~3.0.0"
lodash "^4.17.15"
lodash-es "^4.17.11"
property-expr "^2.0.2"
synchronous-promise "^2.0.13"
toposort "^2.0.2"

0 comments on commit 73a22eb

Please sign in to comment.