Skip to content

Instantly share code, notes, and snippets.

@koorchik
Last active July 3, 2023 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save koorchik/24bc956be3ddceed6ec8e2c23f261561 to your computer and use it in GitHub Desktop.
Save koorchik/24bc956be3ddceed6ec8e2c23f261561 to your computer and use it in GitHub Desktop.
Fastest with strict: remove vs LIVR Benchmark
import Benchmark from "benchmark";
import LIVR from "livr";
import FastestValidator from "fastest-validator";
/* FASTEST VALIDATOR */
const fastestValidatorShema = {
$$strict: "remove",
username: { required: true, type: "string" },
gender: { type: "string", enum: ["male", "female"] },
phone: { type: "string", max: 10 },
password: { required: true, type: "string", min: 10 },
password2: { type: "equal", field: "password" },
};
const fastestValidator = new FastestValidator();
const fastestValidatorCheck = fastestValidator.compile(fastestValidatorShema);
/* LIVR VALIDATOR */
const livrValidatorSchema = {
username: "required",
gender: { one_of: ["male", "female"] },
phone: { max_length: 10 },
password: ["required", { min_length: 10 }],
password2: { equal_to_field: "password" },
};
const livrValidator = new LIVR.Validator(livrValidatorSchema);
livrValidator.prepare();
/* FORM DATA */
function getFormData() {
return {
maliciousField: "123",
username: "john",
gender: "male",
phone: "+22221212",
password: "mypassword1",
password2: "mypassword1",
};
}
var suite = new Benchmark.Suite();
// add tests
suite
.add("Fastest Validator", function () {
fastestValidatorCheck(getFormData());
})
.add("LIVR", function () {
livrValidator.validate(getFormData());
})
.add("Fastest Validator with compile phase", function () {
const fastestValidator = new FastestValidator();
const fastestValidatorCheck = fastestValidator.compile(
fastestValidatorShema
);
fastestValidatorCheck(getFormData());
})
.add("LIVR with prepare/compile phase", function () {
const livrValidator = new LIVR.Validator(livrValidatorSchema);
livrValidator.prepare();
livrValidator.validate(getFormData());
})
// add listeners
.on("cycle", function (event) {
console.log(String(event.target));
})
.run({ async: false });
/****** Results after 3 local runs ******/
// Fastest Validator x 2,983,977 ops/sec ±0.25% (96 runs sampled)
// LIVR x 4,341,520 ops/sec ±0.31% (97 runs sampled)
// Fastest Validator with compile phase x 9,383 ops/sec ±0.35% (96 runs sampled)
// LIVR with prepare/compile phase x 893,601 ops/sec ±0.06% (97 runs sampled)
// Fastest Validator x 2,773,231 ops/sec ±0.19% (93 runs sampled)
// LIVR x 4,176,554 ops/sec ±0.26% (97 runs sampled)
// Fastest Validator with compile phase x 9,141 ops/sec ±0.20% (97 runs sampled)
// LIVR with prepare/compile phase x 921,251 ops/sec ±0.08% (98 runs sampled)
// Fastest Validator x 2,782,221 ops/sec ±0.43% (96 runs sampled)
// LIVR x 4,193,905 ops/sec ±0.34% (93 runs sampled)
// Fastest Validator with compile phase x 9,029 ops/sec ±0.20% (98 runs sampled)
// LIVR with prepare/compile phase x 894,932 ops/sec ±0.12% (96 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment