:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(frontend): measure the cost of zod's JIT parser compiler (#94413)
Tom Piccirellod3463a51866c
diff --git a/frontend/src/index.tsx b/frontend/src/index.tsxindex d46bb9404e6a..ba1191b59769 100644--- a/frontend/src/index.tsx+++ b/frontend/src/index.tsx@@ -12,16 +12,25 @@ import { ChunkLoadErrorBoundary } from './scenes/ChunkLoadErrorBoundary'// Lazy-load App so the entry chunk stays minimal: the entire transitive dependency// graph (kea, posthog-js, scene logic, UI components) is only fetched when it renders.-// bootApp() runs the one-time boot side effects (posthog-js, kea) after the chunks-// load and before <App /> first renders. It lives in its own module so scenes/App-// keeps component-only exports and stays a React Fast Refresh boundary.+// configureZod() is imported and called on its own before the App chunk, because zod+// binds its jitless setting when it constructs each object schema and the App graph+// constructs some at module scope. bootApp() runs the remaining one-time boot side+// effects (posthog-js, kea) after the chunks load and before <App /> first renders.+// It lives in its own module so scenes/App keeps component-only exports and stays a+// React Fast Refresh boundary.const App = lazy(() =>- Promise.all([retryBootImport(() => import('scenes/App')), retryBootImport(() => import('scenes/bootApp'))]).then(- ([appModule, bootModule]) => {+ retryBootImport(() => import('lib/configureZod'))+ .then(({ configureZod }) => {+ configureZod()+ return Promise.all([+ retryBootImport(() => import('scenes/App')),+ retryBootImport(() => import('scenes/bootApp')),+ ])+ })+ .then(([appModule, bootModule]) => {bootModule.bootApp()return { default: appModule.App }- }- )+ }))declare global {diff --git a/frontend/src/lib/configureZod.ts b/frontend/src/lib/configureZod.tsnew file mode 100644index 000000000000..98bd83c57672--- /dev/null+++ b/frontend/src/lib/configureZod.ts@@ -0,0 +1,25 @@+import { z } from 'zod'++import { FEATURE_FLAGS } from 'lib/constants'++/**+ * Turn zod's JIT parser compiler off for a share of users so we can measure what it costs us.+ *+ * zod compiles a parser per schema with `new Function`, which needs `'unsafe-eval'` in `script-src`+ * and is about half of all CSP violations the app reports. The compiler pays for itself only when+ * one schema is parsed many times, and most of ours are parsed a handful of times per page load, so+ * turning it off may well be free or better. Measure rather than assume.+ *+ * The flag is read from the bootstrap Django renders into the page, not from `featureFlagLogic`.+ * zod binds `jitless` when it constructs each object schema, so this must run before any module+ * that builds a schema at module scope evaluates. src/index.tsx imports and calls it before it+ * imports the App chunk. A flag arriving later would leave a mix of compiled and interpreted+ * schemas and a measurement that means nothing.+ *+ * Django renders bootstrap flags only for authenticated users, so anonymous pages such as login+ * and signup keep the compiler whatever the flag says.+ */+export function configureZod(): void {+ const bootstrapFlags = window.POSTHOG_USER_IDENTITY_WITH_FLAGS?.featureFlags+ z.config({ jitless: !!bootstrapFlags?.[FEATURE_FLAGS.ZOD_JITLESS] })+}diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsxindex 6c4e445b9cb8..985eb78fba4f 100644--- a/frontend/src/lib/constants.tsx+++ b/frontend/src/lib/constants.tsx@@ -260,6 +260,7 @@ export const FEATURE_FLAGS = {WEB_ANALYTICS_FOR_MOBILE: 'web-analytics-for-mobile', // owner: #team-web-analyticsWEB_ANALYTICS_PATH_CLEANING_SUGGESTIONS: 'web-analytics-path-cleaning-suggestions', // owner: @lricoy #team-web-analyticsWEB_ANALYTICS_REFERRER_URL_DRILLDOWN: 'web-analytics-referrer-url-drilldown', // owner: @jabahamondes #team-web-analytics+ ZOD_JITLESS: 'zod-jitless', // owner: @Piccirello measuring the cost of zod's JIT parser compiler// Temporary feature flags, still WIP, should be removed eventuallyACTION_REFERENCE_COUNT: 'action-reference-count', // owner: @andyzzhao #team-product-analytics, gates bulk action reference counting on actions listdiff --git a/frontend/src/scenes/bootApp.ts b/frontend/src/scenes/bootApp.tsindex 1a5543f17af2..4153ac94e7c4 100644--- a/frontend/src/scenes/bootApp.ts+++ b/frontend/src/scenes/bootApp.ts@@ -14,6 +14,8 @@ let appBooted = false* Lives outside App.tsx so scenes/App keeps component-only exports and stays a React* Fast Refresh boundary; with a mixed-export App.tsx, HMR invalidations cascade into* src/index.tsx and force a full page reload on routine edits.+ * zod is configured in src/index.tsx before this chunk is imported, not here: by the time+ * this runs, the App graph has already constructed schemas at module scope.*/export function bootApp(): void {if (appBooted) {