Skip to content

Using Mutative with Zod's readonly() schemas #158

@pawelwilczewski

Description

@pawelwilczewski

For my project, I am considering the best way of using Zod's .readonly() schemas with Mutative's development-only auto-freezing.

Please correct me if I'm wrong, but it appears that for optimal performance, we don't want to freeze the objects when using Mutative. However, since my objects are a result of parsing external data, I often have schemas which I declare readonly, i.e.:

export const mySchema = z.object({
    a: someOtherReadOnlySchema,
    b: z.string(),
    c: z.array(mediumSizeReadonlyObjectSchema).readonly(),
  }).readonly();

Since Zod's readonly() freezing behaviour does not change between development and production, I have two questions:

  1. Is it a "big deal" that these initial objects are frozen - do frozen objects perform worse for repeated usage of Mutative on them? Schemas sometimes contain arrays of 50k+ objects, each with 10 keys.
  2. Are there any best practices for this sort of scenario?

I suppose the ideal solution here is matching Zod's readonly behaviour with Mutative's, so it doesn't actually freeze in production, only in development. That doesn't seem to be an option, but if the idea is right, I may implement logic around Zod to facilitate that.

I am open to any suggestions (also swapping out Zod for anything that may be more suitable).

Edit: To answer this for my use case, I could run some benchmarks and compare. However, I am more interested in knowing the suggested approaches that I should default to when using this library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions