Skip to content

Precompilation #810

Closed
Closed

Description

@ssalbdivad

This would allow a set of scopes/types to be precompiled to avoid JIT compilation at runtime.

This requires additional investigation around possible targets (ts vs js build output) and tooling (TS-morph vs some JS AST transformer or other).

Activity

  1. moved this from To do to Backlog in arktypeioon Jun 28, 2023
  2. Stadly commented on Mar 11, 2025

    @Stadly

    Very interesting. Would this be something similar to what Typia does? Though I don't think Typia can handle anything that cannot be expressed as TypeScript types.

  3. ssalbdivad commented on Mar 11, 2025

    @ssalbdivad
    MemberAuthor

    Not exactly. ArkType already uses these kind of JIT optimizations internally so it's really just a matter of outputting them as part of a build process, figuring out constraints around how + where types can be defined, non-serializable constraints etc.

  4. flagpoonage commented on Oct 8, 2025

    @flagpoonage

    This would be great to see. After evaluating ArkType in the realm of browser extensions, the startup performance penalty is a non-starter for us. We use validation heavily (specifically matching with allows in ArkType), to route messages between content scripts and the background service. Extensions communication is done via JSON serialization so we don't retain types between them. We have some fairly ridiculous kind of type unions (470+ possible different strings), nested in even larger objects, which is probably a big reason it's so slow.

    Startup performance is critical though, because content scripts run on every page if the user is running the extension. Here's a performance grab from loading the ArkType docs with our extension enabled. About 14 seconds of a frozen tab with 100% CPU usage.

    Image

    We would much rather take the hit in the build step (which pretty much every serious project will have anyway), rather than passing it on to our users, on every page they visit.

    To be a bit more positive, I think it's a really cool system, and the DX is pretty good!

  5. miyaji255 commented on Nov 27, 2025

    @miyaji255

    Hi, I've been following the discussion on precompilation.

    Have you considered an approach similar to Bun Macros? It allows functions to run at bundle-time and inline the returned value directly into the source. This seems to align perfectly with the goal of zero-runtime overhead for complex schemas.

    I realize this approach requires the return value to be JSON-serializable and leads to vendor lock-in, but I thought it might be worth mentioning as a reference.

  6. miyaji255 commented on Dec 6, 2025

    @miyaji255

    Hi, I've been experimenting with AOT optimization for ArkType using Bun Macros.
    I created a PoC that pre-compiles schemas at build time, and the performance improvement looks significant.

    Here is the repository and the benchmark results:
    https://github.com/miyaji255/arktype-macro-bench

    Task name Latency avg (ns) Throughput avg (ops/s) Margin
    'type - simple object' '90609 ± 5.96%' '12728 ± 0.30%'
    'typeMacro - simple object' '7475.1 ± 1.32%' '142769 ± 0.06%' 11.22x faster
    'type - simple object2' '174605 ± 1.28%' '6145 ± 0.44%'
    'typeMacro - simple object2' '2949.9 ± 1.36%' '408484 ± 0.05%' 66.47x faster
    'type - complex nested' '166255 ± 11.01%' '7036 ± 0.41%'
    'typeMacro - complex nested' '3389.1 ± 1.18%' '371600 ± 0.06%' 52.81x faster
  7. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    MemberAuthor

    Fixed in 3.0.0-beta.0 (#1728). Try it with npm i arktype@next.

  8. moved this from Backlog to Done (merged or closed) in arktypeioon Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Done (merged or closed)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions