Title: perf: exponential slowdown due to type narrowing with enums / type unions · Issue #35205 · microsoft/TypeScript · GitHub
Open Graph Title: perf: exponential slowdown due to type narrowing with enums / type unions · Issue #35205 · microsoft/TypeScript
X Title: perf: exponential slowdown due to type narrowing with enums / type unions · Issue #35205 · microsoft/TypeScript
Description: TypeScript Version: 3.8.0-dev.20191119 Also reproduceable in earlier versions. A quick scan through older versions shows TS 2.0 has a check time of 0.2s, which regressed to 1.9s in TS 2.1. Search Terms: performance, union types, enum, fl...
Open Graph Description: TypeScript Version: 3.8.0-dev.20191119 Also reproduceable in earlier versions. A quick scan through older versions shows TS 2.0 has a check time of 0.2s, which regressed to 1.9s in TS 2.1. Search T...
X Description: TypeScript Version: 3.8.0-dev.20191119 Also reproduceable in earlier versions. A quick scan through older versions shows TS 2.0 has a check time of 0.2s, which regressed to 1.9s in TS 2.1. Search T...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/35205
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"perf: exponential slowdown due to type narrowing with enums / type unions","articleBody":"**TypeScript Version:** 3.8.0-dev.20191119\r\n\r\nAlso reproduceable in earlier versions. A quick scan through older versions shows TS 2.0 has a check time of 0.2s, which regressed to 1.9s in TS 2.1.\r\n\r\n**Search Terms:** performance, union types, enum, flow analysis, type narrowing\r\n\r\n**Code**\r\n\r\n```ts\r\nexport enum Choice {\r\n One,\r\n Two,\r\n}\r\n\r\nconst choice: Choice = Choice.One;\r\nconst choiceOne = Choice.One;\r\n\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\nif (choice === choiceOne) {}\r\n```\r\n\r\n**Expected behavior:**\r\nType checking of this single file completes in the order of milliseconds.\r\n\r\nA real world example of this issue has been reported in the Angular repo for code generated by the template type checker: https://github.com/angular/angular/issues/33532. Angular has a code generator that emits TypeScript code to typecheck Angular templates.\r\n\r\n**Actual behavior:**\r\n\r\nEach additional if-statement is causing the type checking phase to roughly double in time. The above sample takes ~2.9s in the \"Check\" phase, as reported in the diagnostics report:\r\n\r\n```\r\nFiles: 2\r\nLines: 4381\r\nNodes: 13211\r\nIdentifiers: 5483\r\nSymbols: 3528\r\nTypes: 1225\r\nMemory used: 42102K\r\nI/O read: 0.00s\r\nI/O write: 0.00s\r\nParse time: 0.12s\r\nBind time: 0.04s\r\nCheck time: 2.87s\r\nEmit time: 0.00s\r\nTotal time: 3.03s\r\n```\r\n\r\nAdding one additional `if` statement increases the Check time to ~6.5s:\r\n\r\n```\r\nFiles: 2\r\nLines: 4382\r\nNodes: 13217\r\nIdentifiers: 5485\r\nSymbols: 3528\r\nTypes: 1225\r\nMemory used: 49768K\r\nI/O read: 0.00s\r\nI/O write: 0.00s\r\nParse time: 0.13s\r\nBind time: 0.05s\r\nCheck time: 6.43s\r\nEmit time: 0.00s\r\nTotal time: 6.61s\r\n```\r\n\r\nThese options **do** resolve the exponential slowdown:\r\n- Changing the type of either `choice` or `choiceOne` to `any`\r\n- inlining `choiceOne` into the if-statements\r\n\r\nThese options do **not** resolve the exponential slowdown:\r\n- Reducing the enum to a single option\r\n- Changing the type of choice into `Choice.One`\r\n- Replacing the enum with a type union\r\n\r\nNote that all these options do reduce the time, however adding additional if-statements still shows exponential slowdown.\r\n\r\n**Playground Link:**\r\n\r\nTerminal reproduction to get insight in timings:\r\n```\r\nnpx typescript@3.8.0-dev.20191119 sample.ts --diagnostics --noEmit --lib es5 --strict false --typeRoots []\r\n```\r\n\r\nSource code for `sample.ts` is found above, under _Code_.\r\n\r\n**Related Issues:** Not really","author":{"url":"https://github.com/JoostK","@type":"Person","name":"JoostK"},"datePublished":"2019-11-19T20:00:03Z","url":"https://github.com/microsoft/TypeScript/issues/35205"}
| 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:73be80db-be99-a1bb-0362-6fc0e1329591 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F68:149F72:17BEB1:1FAD42:6B016395 |
| html-safe-nonce | 3f068336cf9d6bb81d8ddae835c036ba085fa1b7d22ba447f9f0757f1caa7d2b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjY4OjE0OUY3MjoxN0JFQjE6MUZBRDQyOjZCMDE2Mzk1IiwidmlzaXRvcl9pZCI6IjI0MDg2MTg1OTMzOTk5NTYzNzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bee0264392ccad340f6483e175a7c25e92740457e295d7f6fac842c65f87a81a |
| hovercard-subject-tag | issue:525233088 |
| 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/35205/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f87dd5428e6d48e2539c56b31de5d131087f942d5305aac2f230b692977875cd/microsoft/TypeScript/issues/35205 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f87dd5428e6d48e2539c56b31de5d131087f942d5305aac2f230b692977875cd/microsoft/TypeScript/issues/35205 |
| og:image:alt | TypeScript Version: 3.8.0-dev.20191119 Also reproduceable in earlier versions. A quick scan through older versions shows TS 2.0 has a check time of 0.2s, which regressed to 1.9s in TS 2.1. Search T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JoostK |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width