Skip to content

moltar/typescript-runtime-type-benchmarks

Repository files navigation

πŸ“Š Benchmark Comparison of Packages with Runtime Validation and TypeScript Support


⚑⚠ Benchmark results have changed after switching to isolated node processes for each benchmarked package, see #864 ⚠⚑


Benchmark Results

Fastest Packages - click to view details

click here for result details

Packages Compared

Criteria

Validation

These packages are capable of validating the data for type correctness.

E.g. if string was expected, but a number was provided, the validator should fail.

Interface

It has a validator function or method that returns a valid type casted value or throws.

const data: any = {}

// `res` is now type casted to the right type
const res = isValid(data)

Or it has a type guard function that in a truthy block type casts the value.

const data: any = {}

function isMyDataValid(data: any) {
  // isValidGuard is the type guard function provided by the package
  if (isValidGuard(data)) {
    // data here is "guarded" and therefore inferred to be of the right type
    return data
  }

  throw new Error('Invalid!')
}

// `res` is now type casted to the right type
const res = isMyDataValid(data)

Local Development

  • npm run start - run benchmarks for all modules
  • npm run start run zod myzod valita - run benchmarks only for a few selected modules
  • npm run docs:serve - result viewer
  • npm run test - run tests on all modules

Adding a new node version

  • update node version matrix in .github/workflows/pr.yml and .github/workflows/release.yml
  • update NODE_VERSIONS in docs/dist/app.tsx and run npm run docs:build
  • optionally set NODE_VERSION_FOR_PREVIEW in benchmarks/helpers/main.ts

Test cases

  • Safe Parsing

    • Checks the input object against a schema and returns it.
    • Raises an error if the input object does not conform to the schema (e.g., a type mismatch or missing attribute).
    • Removes any extra keys in the input object that are not defined in the schema.
  • Strict Parsing

    • Checks the input object against a schema and returns it.
    • Raises an error if the input object does not conform to the schema (e.g., a type mismatch or missing attribute).
    • Raises an error if the input object contains extra keys.
  • Loose Assertion

    • Checks the input object against a schema.
    • Raises an exception if the input object does not match the schema.
    • Allows extra keys without raising errors.
    • Returns true if data is valid.
  • Strict Assertion

    • Checks the input object against a schema.
    • Raises an exception if the input object does not match the schema.
    • Raises an error if the input object or any nested input objects contain extra keys.
    • Returns true if data is valid.