npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@duplojs/zod-accelerator

v2.5.0

Published

[![NPM version](https://img.shields.io/npm/v/@duplojs/zod-accelerator)](https://www.npmjs.com/package/@duplojs/zod-accelerator)

Downloads

473

Readme

@duplojs/zod-accelerator

NPM version

Installation

npm i @duplojs/zod-accelerator

Benchmark

Benchmarck result

How to use it

import * as zod from "zod";
import {ZodAccelerator} from "@duplojs/zod-accelerator";

const zodSchema = zod.object({
    firstname: zod.string(),
    lastname: zod.string(),
    age: zod.number(),
    email: zod.string(),
    gender: zod.enum(["boy", "girl"]),
    connected: zod.boolean(),
    createdAt: zod.date(),
    addresse: zod.object({
        postCode: zod.string(),
        city: zod.string(),
        number: zod.number()
    }),
}).array();

const zodAccelerateSchema = ZodAccelerator.build(zodSchema);

const inputData = Array.from({length: 10}).fill({
    firstname: "  Mike ",
    lastname: "ee",
    age: 21,
    email: "[email protected]",
    gender: "girl",
    connected: true,
    createdAt: new Date(),
    addresse: {
        postCode: "22778",
        city: "Paris",
        number: 67
    },
});

const outputData = zodAccelerateSchema.parse(inputData);

When should I use it

ZodAccelerator is useful when the schema is used multiple times during the same session. The time to build the custom function is about twice as long as its execution. If you only need to call your schema once, it might not be the best solution.

How does it work

The ZodAccelerator.build function allows you to create a custom function based on the schema passed as an argument. The function will be used to create an instance of the ZodAcceleratorParser object. This makes its usage similar to Zod's.

zodSchema.parse(...)
zodAccelerateSchema.parse(...)

zodSchema.parseAsync(...)
zodAccelerateSchema.parseAsync(...)

zodSchema.safeParse(...)
zodAccelerateSchema.safeParse(...)

zodSchema.safeParseAsync(...)
zodAccelerateSchema.safeParseAsync(...)

Support for Zod types

To build the custom function, we have created a converter for each type. Not all types are supported. However, to avoid compatibility issues, when a type is not supported, ZodAccelerator directly uses the Zod schema. To find out which types are supported, go to the scripts/accelerators folder.

The differences

The functions created by ZodAccelerator have some key differences compared to Zod's basic functionality :

  • In case of an issue, the schema analysis does not continue, it stops immediately.
  • Errors are not as explicit.
  • All secondary parameters, such as custom messages, may not be implemented everywhere (can be done on request).
  • The Zod contexts of zodEffect are not as complete.

How custom functions are created

For each type that Zod provides, we have an algorithm that translates the information from the Zod schema into a string of code. At the end, the different relevant parts are assembled and interpreted using the eval function (no injection possible).

Caching system?

Would it be possible to build the schemas in a file and then call them instead of rebuilding them? Unfortunately, no. The construction is mandatory because, in addition to the content, we also build its context (this). In the case of a ZodEffect type, for example, the associated function is stored in the context. Therefore, skipping the construction phase is not possible.

Skipping eval ?

As mentioned earlier, ZodAccelerator uses the eval function to interpret the code after a Node program is launched. Currently, no other method seems viable.