Repository navigation
Opportunity: Improve compiler performance by avoiding property accesses #39247
Description
Activity
DanielRosenwasser commented
on Jun 25, 2020 MemberMore actionsAndrew Casey (@amcasey) Anders Hejlsberg (@ahejlsberg) Wesley Wigham (@weswigham), it'd be nice to see if #38510 gives any observable performance boost.
Reacted by Marvin Hagemeister#35561 is trying to use modules instead of namespaces
Reacted by Daniel Rosenwasser, Evan Wallace, Wesley Wigham, Amin Ya and mk1960hdkDanielRosenwasser commented
on Jun 25, 2020 MemberMore actionsWhoops, that's the right link, thanks Mohsen Azimi (@mohsen1).
- addedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Jun 25, 2020 Cool! Mohsen Azimi (@mohsen1) I assume that if you run the tests in the other order (i.e. no-namespaces first), you get the same result? I've gotten into the habit of discarding the first run whenever I do perf testing to minimize the effects of disk caching.
I did my own run against
ant-designand saw a ~5% reduction in total compilation time. Pretty impressive. I have yet to compare the compiler output (i.e. to confirm it hasn't changed).Reacted by LifeIsStrange and Peter Lai- addedDomain: PerformanceReports of unusually slow behaviorReports of unusually slow behaviorSuggestionAn idea for TypeScriptAn idea for TypeScriptand removedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Nov 19, 2021 DanielRosenwasser commented
on Nov 19, 2021 MemberMore actionsThis should go hand-in-hand with the modules work for 4.6. I would hope that a bundler with scope hoisting will allow us to avoid property accesses/function calls that serve no purpose.
Reacted by Evan Wallace, Wenzhao Hu and Rui- addedFix AvailableA PR has been opened for this issueA PR has been opened for this issue
on Nov 2, 2022 For those following this issue, #51387 will fix this (though it was later than 4.6 😄).
Reacted by Rui and iron-cherep- added a commit that references this issue
on Nov 7, 2022

TypeScript Version: 3.9.5
Search Terms: compiler performance namespace run-time property access
Code
I have looked into TypeScript compiler performance in the past and I've always wondered why the TypeScript compiler code uses namespaces heavily, since run-time property accesses are slower than statically-bound identifiers in JavaScript.
I finally got around to writing a proof of concept:
This post-processes
tsc.jsto convert run-time property accesses into statically-bound identifiers. I ran this on the Rome code as a benchmark of a reasonably-large TypeScript code base and got a noticeable speed boost:tsc.jsEach time is the best of 5 runs, and each run was
time node node_modules/typescript/lib/tsc.js -noEmit -project rome.The TypeScript compiler is leaving some performance on the table by using run-time property accesses where it could use statically-bound identifiers instead. I'm sure you are using namespaces for code organization for good reasons, but it does come at a cost. An alternative to namespaces that might have less performance overhead could be to use ES6 modules and bundler, for example.
I'm posting this issue because I think the results of this experiment are interesting. The issue tracker seemed like the most appropriate place to post this. Feel free to just close this issue if you'd like.