Skip to content

perf: inline feature bits inside the library - #183

Open
LadyBluenotes wants to merge 2 commits into
lxsmnsyc:mainfrom
LadyBluenotes:perf/feature-const-object
Open

LadyBluenotes wants to merge 2 commits into
lxsmnsyc:mainfrom
LadyBluenotes:perf/feature-const-object

Conversation

@LadyBluenotes

@LadyBluenotes LadyBluenotes commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Every Feature.X read inside the library now goes through an internal const enum (FeatureFlag), so each check inlines to a literal instead of a property read on the enum object. The exported Feature stays a runtime enum, byte for byte the same declaration as before, so Feature.X reads, bitwise combinations, Feature[1] reverse lookups and Feature as a type all keep working. A test asserts the public values match the internal bits and that they cover ALL_ENABLED.

An earlier revision of this PR replaced the public enum with an as const object plus a union type. That changed the published declaration from declare enum Feature to a const and a type, which is an API change for anyone typing a bit combination as Feature, so it was reverted here. The size win from that revision only applied to bundles that import Feature (about 40 B gzip), and the internal inlining is kept.

Measured as minified browser ESM consumer bundles (ES2020) with esbuild 0.28.2 and Rolldown 1.2.5, gzip level 9 and Brotli quality 11, compared with a054acc: serialize, the client set and the server set are unchanged within a few bytes, since the unused enum was already tree-shaken by the @__PURE__ annotation on its IIFE.

On Node 24.12.0, serialize, toJSON, fromJSON and fromCrossJSON over a 300-object graph (dates, maps, sets, typed arrays, errors) stay within ±2% of a054acc across five alternating samples of 60 calls, which is inside run-to-run noise.

Tests: 907 passing.

Internal code uses an inlined const enum; the public Feature object keeps
the forward mapping only, so bundles that never import it carry nothing
and bundles that do skip the reverse-mapping IIFE.
@changeset-bot

changeset-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b7b9088

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
seroval Patch
seroval-plugins Patch
seroval-benchmarks Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@LadyBluenotes
LadyBluenotes force-pushed the perf/feature-const-object branch from 39b8c49 to dbaf984 Compare September 11, 2026 21:57
lxsmnsyc
lxsmnsyc previously approved these changes Sep 12, 2026
@lxsmnsyc

Copy link
Copy Markdown
Owner

This will hit Solid because of the rename.

Restore the exported runtime enum so the published declaration is unchanged, and keep the internal const enum for inlining.
@LadyBluenotes LadyBluenotes changed the title perf: export Feature as a constant object perf: inline feature bits inside the library Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants