Title: Optimise __extends · Issue #9925 · microsoft/TypeScript · GitHub
Open Graph Title: Optimise __extends · Issue #9925 · microsoft/TypeScript
X Title: Optimise __extends · Issue #9925 · microsoft/TypeScript
Description: I was trying to find places to optimise a large project, and realised that the longest running function was actually the __extends call, which prompted me to look at the current code. Current code Two things immediately jumped out at me ...
Open Graph Description: I was trying to find places to optimise a large project, and realised that the longest running function was actually the __extends call, which prompted me to look at the current code. Current code ...
X Description: I was trying to find places to optimise a large project, and realised that the longest running function was actually the __extends call, which prompted me to look at the current code. Current code ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9925
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Optimise __extends","articleBody":"I was trying to find places to optimise a large project, and realised that the longest running function was actually the __extends call, which prompted me to look at the current code.\n# Current code\n\nTwo things immediately jumped out at me with the current code:\n\n``` javascript\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};\n```\n# Problems\n## Potentially unused code (pre ES5)\n\nThe first problem was that the `__()` function was being created, when it might not be used (though chances of that are probably very small).\n\nThe Solution would be to move the check into an if/else. Related to this is that `null` doesn't have any properties, so the property copying should be in the \"not null\" section.\n\n``` javascript\nvar __extends = (this && this.__extends) || function (d, b) {\n if (b===null) {\n d.prototype = Object.create(b);\n } else {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n var __=function() { this.constructor = d; };\n __.prototype = b.prototype;\n d.prototype = new __();\n }\n};\n```\n## Inefficient iteration (for ES5+)\n\nThe second problem is purely performance related with the property copying. `for(var p in b)` will iterate over the entire prototype tree, but `if(b.hasOwnProperty(p))` will then skip any that aren't directly on the object/class itself. (see [Enumerability and ownership of properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties))\n\nThe solution would be to use `Object.keys(b)` and iterating an array, which will work only on the object/class itself (and not it's prototype tree), it also automatically skips the same keys we'd be skipping anyway - so taking most of what we're wanting to the browser / native code instead of JS.\n\nUnlike `for...in`, Object.keys fails when called on `null`, `undefined` or any other non-Object, so this requires some extra code to help prevent that happening.\n\nFrom [here](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Specifications) we see that `Object.keys()` is an ES5+ function.\n\nSuggested code:\n\n``` javascript\nvar __extends = (this && this.__extends) || function (d, b) {\n if (b instanceof Object) {\n Object.keys(b).forEach(function(p){\n d[p] = b[p];\n });\n var __=function() { this.constructor = d; };\n __.prototype = b.prototype;\n d.prototype = new __();\n } else {\n d.prototype = Object.create(null);\n }\n};\n```\n# Results\n\nIt's very hard to get reliable timings for the code on Chrome Profiling, but on average this looks like roughly a 10% speedup at startup for my project (38 classes in a nested tree up to six deep). There is no major benefit to this, though it does make the code slightly \"safer\".\n\nThe ES5+ code could potentially cause different behaviour if a class tries to inherit from a non-class (such as a number or string), though I'm not sure that is possible to begin with..\n# Language Feature Checklist\n- Syntactic: No changes as this is a compiler change.\n- Semantic: No changes as the final code does the same thing when used as intended (see Results above).\n- Emit: Output code will change depending on ES version needed, both versions are slightly larger than the original.\n- Compatibility: Two versions, one for pre-ES5, most efficient for ES5+\n- Other: There is already an option to prevent __extends being output (which only happens if needed anyway). This could potentially hit older build scripts that manually search & replace the old code.\n","author":{"url":"https://github.com/Rycochet","@type":"Person","name":"Rycochet"},"datePublished":"2016-07-25T10:52:09Z","url":"https://github.com/microsoft/TypeScript/issues/9925"}
| 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:aca79df1-f074-241e-5e06-5dfd0d5f7c4c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEEE:229B2E:847FE6:B14712:6B00DD64 |
| html-safe-nonce | 385a3081ee445cf345be46cf6533e22b880ca1b4caca32182dc262909112f2c2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUVFOjIyOUIyRTo4NDdGRTY6QjE0NzEyOjZCMDBERDY0IiwidmlzaXRvcl9pZCI6IjE1MjI0MDY1NDM0NjU3MDA3MDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 376ae36e5020e966566f6a381741be9ce90c8983163d639683a852bec6a05f12 |
| hovercard-subject-tag | issue:167338867 |
| 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/9925/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f0aedc232250930aa5c099314967ded59183f0ef005ed5ef1385ffc59ec59887/microsoft/TypeScript/issues/9925 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f0aedc232250930aa5c099314967ded59183f0ef005ed5ef1385ffc59ec59887/microsoft/TypeScript/issues/9925 |
| og:image:alt | I was trying to find places to optimise a large project, and realised that the longest running function was actually the __extends call, which prompted me to look at the current code. Current code ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Rycochet |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width