use tree-shaking to reduce the bundle size by >90% #614
Replies: 1 comment 6 replies
-
@samuelstroschein Hiya, Bundle SizesJust for reference, the following table are the current bundling sizes for various libraries.
Note that the TypeBox result above relates specifically to the TypeBuilder (which is currently non-shakable). ESM ModulesThe TypeBox TypeBuilder (everything inside The anticipated import scheme post split will be: import Type, { Static } from '@sinclair/typebox' Given this is a breaking change, there is some reluctance to enact this change until everything is finalized for 1.0 (and that this scheme will be supported into the future). So this is planned for, just not for a while. Value Modules
You won't get to 100 bytes, but you can significantly reduce the size of the Value modules by importing each function individually. import { Check } from '@sinclair/typebox/value/check' // > 24 kb Note: TypeBox does need to import logic to check for all types (as it can't know in advance what types are going to passed to it). Work was done on the 0.30.x and 0.31.x revisions to reduce output size (many code level optimizations), as well as to make each function with it's associated infrastructure individually importable. Outside of the above, there is very little else that can be done while keeping schematics and validation logic separated (which is a key design principle for the library). However, there may be room to explore more optimal (bundler friendly) Json Schema validation libraries external to TypeBox. It would be interesting to see just how minimal a Json Schema implementation could get. Hope this brings some insight |
Beta Was this translation helpful? Give feedback.
-
Problem
Typebox ships too large bundle sizes for resource constraint environments. Aka typebox can't be used.
The following code ships 36.3kb [source]
Proposal
Leverage ESM tree-shaking to decrease bundle sizes by over 90%.
For comparison, valibot, which leverages tree-shaking, bundles 703 bytes for the same code [source]. A bundle size reduction of 98% compared to typebox.
Examples
The following code would only import
Object
andString
validators without all availableType.*
stuff.The syntax of
Type.*
could even be sustained with namespace imports which are tree-shaken by bundlers too.Additional information
The
Value
import of typebox is too large as well, counting >70kb that are included in the bundle size. Valibot's equivalentparse
bundles only 100 bytes.Beta Was this translation helpful? Give feedback.
All reactions