Lately, there was a lot of great articles about starting with TypeScript (from Scott Durow, Oliver Flint, Benedikt Bergmann to name a few). Those are great articles and I definitely advise you to check them out. In this article we will not talk about how you can set up your ts project, rather we will talk about performance, unnecessary code and target version. Hope this article will be interesting not only for Power Platform folks but for developers in general.
tsconfig file has a lot of options that allow you to control every bit of the typescript behaviour. One important piece is the target property. According to official docs:
Target - Allows to specify ECMAScript target version.
Basically, you select to which ECMAScript version your code will be transpiled. In most cases, you will see es5 as a default option. And this is understandable - back in the day es5 features were supported by most browsers including IE and offered great functional features.
However, right now, according to Google, 95% of global web traffic comes from web sites that support at least es2017 feature set. This means that we can target es2017 as our transpile version and be pretty confident that it will be supported.
What if you need to support older browsers like IE 11? Well if we are talking about Power Platform space Microsoft will deprecate Dynamics support for IE11 in August 2021 (see here) so you will need to update and that's a good time to improve code as well. If we are talking about web developers in general there is an approach of generating legacy code and set that as a fallback. Please check this article from Google for more information.
ES5 vs ES2017
Let’s review how the change only in the target version will improve our code.
Below you can find sample ts code for the contact form. It consists of two async function - first runs on onload event and calls the second one to fetch the number of employees from parent account and then present it in the alert screen.
Now we will compile it to js using default tsconfig (with es5 as target)
This is our result:
ES5 results overview
That’s a lot of code. 20 lines of original code transpiled into 75 lines of output code (size of the result file is 4.11 KB). That’s a lot of additional code (375% increase in the number of lines). And for the bigger code, this increase will be even bigger.
Now we will update tsconfig to es2017 and see how much improvement we got.
This is the result for es2017:
ES2017 results overview
As you can see our js code looks much better - only 20 lines (same number as in original typescript code) and the size of the output file is only 907 bytes. This is an enormous improvement vs es5 version. This is because es2017 brought native support for async/await operations and the biggest chunk of additional code in es5 version comes from the support of those operations in older browsers. If we would write our code with async operations improvement might be not that great but still be present.
And it is not only the number of lines of code and the size of the output file that matters but also the number of instructions that need be executed by the system. Transpiling to a newer version of the code will ensure that fewer instructions will be generated thus improving the speed of resulting code. For example, in this video, Google shows how simple one line of code that produces only 62 instructions after transpiling to the older es version will produce 1173 instructions, which will slow down the execution (6 times in this particular example).
When you develop a project for web (either site, an app or D365 web resource) performance of the code and its size matters, as it could lead to improvements in the user usage experience. So if you work with Typescript files be sure to select proper target version as it can help you to ship more modern and thus more powerful code. At the moment of writing this article ES2017 is an ideal spot to cover as much web browsers as possible and at the same time ensure good code performance.
If you liked this article, please subscribe so you don't miss any future post.