Title: Unexpected slowness initializing large array with discriminated union constituents · Issue #42522 · microsoft/TypeScript · GitHub
Open Graph Title: Unexpected slowness initializing large array with discriminated union constituents · Issue #42522 · microsoft/TypeScript
X Title: Unexpected slowness initializing large array with discriminated union constituents · Issue #42522 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms union array initialization performance 🕗 Version & Regression Information This is the behavior in every version I tried since 3.3. No meaningful perf changes were observed depending on TS version ⏯ Playground Li...
Open Graph Description: Bug Report 🔎 Search Terms union array initialization performance 🕗 Version & Regression Information This is the behavior in every version I tried since 3.3. No meaningful perf changes were observed...
X Description: Bug Report 🔎 Search Terms union array initialization performance 🕗 Version & Regression Information This is the behavior in every version I tried since 3.3. No meaningful perf changes were obse...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42522
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unexpected slowness initializing large array with discriminated union constituents","articleBody":"# Bug Report\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n### 🔎 Search Terms\r\n\r\nunion array initialization performance\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried since 3.3. No meaningful perf changes were observed depending on TS version\r\n\r\n### ⏯ Playground Link\r\n\r\nToo large; here's a spreadsheet\r\n\r\nhttps://docs.google.com/spreadsheets/d/1mk6oEwD8YrycBeSNDX4gaM9rxVULgNB_ljAGrQkned8/edit?usp=sharing\r\n\r\n\r\n### 💻 Code\r\n\r\nInstead of 3 constituents, the actual repro has 500 (this is approximately the number found in the real repro) for each example.\r\n\r\nSetup:\r\n```ts\r\ntype Action =\r\n | Action1\r\n | Action2\r\n | Action3;\r\n\r\ntype Action1 = { type: 1, payload: number };\r\ntype Action2 = { type: 2, payload: number };\r\ntype Action3 = { type: 3, payload: number };\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nChecking with `--skipLibCheck --strict --extendedDiagnostics`, we see some undesirable perf variances here.\r\n\r\nThe \"`const`\" version checks in **0.6s**:\r\n```ts\r\nconst p1: Action = { type: 1, payload: 42 };\r\nconst p2: Action = { type: 2, payload: 42 };\r\nconst p3: Action = { type: 3, payload: 42 };\r\n```\r\n\r\nThe \"basic array\" version checks in **1.0s**:\r\n```ts\r\nconst test: Action[] = [\r\n { type: 1, payload: 42 },\r\n { type: 2, payload: 42 },\r\n { type: 3, payload: 42 }\r\n];\r\n```\r\n\r\nThe \"poisoned array\" version (which is closest to the customer repro) checks in **1.3s**:\r\n```ts\r\ndeclare const a: Action;\r\nconst test: Action[] = [\r\n a,\r\n { type: 1, payload: 42 },\r\n { type: 2, payload: 42 },\r\n { type: 3, payload: 42 }\r\n];\r\n```\r\n\r\nThe \"array with extra target\" version checks in **1.6s**:\r\n```ts\r\nconst test: Array\u003cAction | string> = [\r\n { type: 1, payload: 42 },\r\n { type: 2, payload: 42 },\r\n { type: 3, payload: 42 }\r\n];\r\n```\r\n\r\nThe \"unannotated array\" version checks in **0.07s** (obviously not a target, but shows that these perf numbers aren't cause by \"other work):\r\n```ts\r\nconst test = [\r\n { type: 1, payload: 42 },\r\n { type: 2, payload: 42 },\r\n { type: 3, payload: 42 }\r\n];\r\n```\r\n\r\n\r\n### 🙂 Expected behavior\r\n\r\nAll of the array cases should be checked *approximately* as fast as the const version, not >2x slower.\r\n\r\nOther notes:\r\n * Using a string discriminant in place of number doesn't change anything\r\n * The original repro is even larger\r\n * Timing numbers are on my i7-10700K (i.e. a *very* fast computer by 2021 standards)\r\n","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2021-01-27T22:04:54Z","url":"https://github.com/microsoft/TypeScript/issues/42522"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:acdbdb77-9f20-7d96-0350-3c8b1d734bb4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ACF2:18C3B1:170680:205128:6B047092 |
| html-safe-nonce | 39e1e2f4fbe00b2e52606d618d882923a43483cb363026e7cccb02ea3735c81e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0YyOjE4QzNCMToxNzA2ODA6MjA1MTI4OjZCMDQ3MDkyIiwidmlzaXRvcl9pZCI6Ijg5NTU0MTg2NDU0ODQ1NjQ2MjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d3fa46b5c9d5866a024cd55447ea523618fc09ff23c231ba80a89e68b990adb0 |
| hovercard-subject-tag | issue:795481401 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/42522/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8463d8a494be32e9d8cf4534b6d2a1bd973e1e1bf40427af92d8cb23b3512104/microsoft/TypeScript/issues/42522 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8463d8a494be32e9d8cf4534b6d2a1bd973e1e1bf40427af92d8cb23b3512104/microsoft/TypeScript/issues/42522 |
| og:image:alt | Bug Report 🔎 Search Terms union array initialization performance 🕗 Version & Regression Information This is the behavior in every version I tried since 3.3. No meaningful perf changes were observed... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width