🌳 perf: make the library actually tree-shakable
Importing one decorator pulled in the message and validator of all 68 — 4,909 bytes instead of 1,837 through esbuild, 4,823 instead of 1,818 through webpack. Nothing failed and nothing warned. The library was simply about three times heavier than it needed to be in every consumer's bundle. Every rule is a top-level call: `export const IsString = rule(...)`. rollup proves such a call side-effect-free by reading the factory, which is why it was already emitting 1,823 bytes — and why a single-bundler measurement would have shown no problem at all. esbuild and webpack do not do that analysis and keep the call. Thirty declarations now carry /*#__PURE__*/, which tsc preserves into the ESM emit, and all three bundlers now land within 20 bytes of each other. Measured, minified, esbuild / rollup / webpack: flattenErrors 287 / 292 / 291 one decorator 1837 / 1823 / 1818 validateSync 3722 / 3554 / 3823 toPlainSync 7744 / 7769 / 7832 toInstanceSync 7900 / 7942 / 7956 a typical DTO 10395 / 10402 / 10372 everything 26266 / 25671 / 26879 The serializer and deserializer drop independently — read JSON and you do not pay for writing it. Both mapping entry points keep the validator, because `validate` defaults to true and that is a real reference rather than a missed optimisation. The annotations are a promise to the bundler, so I checked the three factories they cover: rule, pattern and affix each return a closure and touch nothing outside themselves. A false promise here would mean silent deletion in someone else's production build. src/treeshake.test.ts pins the property. It asserts content rather than only bytes — it names the rules that must not appear — and one case asserts everything IS present when everything is used, so a "shaken" result cannot come from a bundle that failed to build. That mattered: two earlier passes at this measurement reported a clean sweep of shaken symbols because rollup had failed to resolve its entry and grep was reading missing files as absence. Strip the annotations and the test fails with `"must be a latitude" should have been shaken out`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SAcqrz3FcadkYr3xG32CjK
This commit is contained in:
@@ -494,6 +494,34 @@ costs a few percent on `toPlain`, which is the price of never emitting `{}` wher
|
||||
used to be; primitives are handled inline and the check is skipped for arrays and dates, so
|
||||
it is one `Symbol.toStringTag` read per object.
|
||||
|
||||
## Bundle size
|
||||
|
||||
Cereale tree-shakes. Every rule is declared so that a bundler can drop the ones you did not
|
||||
import, which matters for a library with 68 decorators — you pay for what you name and nothing
|
||||
else. Minified bytes, measured through three bundlers, and
|
||||
[pinned by a test](src/treeshake.test.ts) so it cannot quietly regress:
|
||||
|
||||
| What you import | esbuild | rollup | webpack |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| `flattenErrors` | 287 | 292 | 291 |
|
||||
| one decorator | 1,837 | 1,823 | 1,818 |
|
||||
| `validateSync` | 3,722 | 3,554 | 3,823 |
|
||||
| `toPlainSync` | 7,744 | 7,769 | 7,832 |
|
||||
| `toInstanceSync` | 7,900 | 7,942 | 7,956 |
|
||||
| a typical DTO — 5 decorators, map, validate, format errors | 10,395 | 10,402 | 10,372 |
|
||||
| the whole library | 26,266 | 25,671 | 26,879 |
|
||||
|
||||
The serializer and the deserializer drop independently: read JSON and you do not pay for
|
||||
writing it. The validator is kept by both, because `validate` defaults to `true` and the entry
|
||||
points reference it whatever a given call site passes.
|
||||
|
||||
This did not come for free. Every rule is a top-level call — `export const IsString = rule(…)` —
|
||||
and rollup can prove such a call side-effect-free by reading the factory, but esbuild and
|
||||
webpack will not. Without a `/*#__PURE__*/` annotation on each of them, importing one decorator
|
||||
pulled in the message and validator of all 68: **4,909 bytes instead of 1,837**. Nothing failed;
|
||||
the library was simply three times heavier in every consumer's bundle, and the only way to find
|
||||
out was to measure.
|
||||
|
||||
## Notes and Limitations
|
||||
|
||||
- **Rules are checked, types are not inferred.** You write both the field type and the rule;
|
||||
|
||||
Reference in New Issue
Block a user