3

What is the rationale behind having JiT compilation for Angular 2 HTML templates in the browser during run-time?

I know, that Ahead-of-time compilation exist to address this problem, and it improves the start-up performance drastically.

I'm not asking if I should use JiT or AoT compilation.

TypeScript compiler is capable of compiling JSX, does that mean, that someday we are getting the same support for Angular 2 templates as a replacement for @angular/compiler-cli?

halfzebra
  • 6,771
  • 4
  • 32
  • 47

1 Answers1

2

production

This is required if components are created dynamically at runtime, for example when the template markup is loaded from a database.

I think such an approach should be avoided but there are use cases that are difficult to solve otherwise.

There were also discussions that AoT causes larger code size for some applications which eats up the shorter initialization time required with AoT compiled components.

What is the best option for your use case depends on your application and also on the optimizations the Angular2 team will be able to accomplish (I'm pretty sure there are lots of ideas they are experimenting with to get smaller build output and shorter initialization time)

See also How to realize website with hundreds of pages in Angular2

development It is also convenient during development because edit-reload cycles are faster, but for production (deployment) you usually want AoT.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for the answer! This sounds like a very specific use-case for having JiT as a default way to serve templates. – halfzebra Sep 29 '16 at 06:54
  • 1
    Its convenient during development because edit-reload cycles are faster but for production (deployment) you usually want AoT. – Günter Zöchbauer Sep 29 '16 at 07:00
  • This way of putting it sound more reasonable to me, could you please update your answer? – halfzebra Sep 29 '16 at 07:51
  • 1
    This is all great, but I still do not get why JIT cannot be simply used in the bundling process. Why do we need a completely different stack for AoT? Also for AoT you have to write your code different than for JIT. I think this is a bad developer experience. I just cannot get any info on the background of this added (seemingly unnecesary) complexity. So is there an explanation? – Sobvan Nov 18 '16 at 20:59
  • @Szobi You are correct. JIT is a term borrowed from the programming language space (not the framework space) that Angular uses by analogy. Unfortunately, the analogy is incorrect because, as you point out, you have to write different code. Long story short is it is a design question with a subjective answer. Since this bifurcation is by no means a common practice, it is all the more incumbent on you to ask yourself if you agree with the tradeoffs of this design decision and how you feel about the implications as to the overall design that it speaks to. – Aluan Haddad Feb 27 '17 at 06:44
  • That's because Angular also uses the term "compilation" (for compiling templates). "different code" is IMHO also misleading. AoT only supports a subset of TS/JS because it needs to evaluate the code result without actually executing the code. TS/JS doesn't provide any support for that. In Dart for example, requiring the expressions to be `const` which means it can be evaluated statically, which makes it clear which subset of the language can be used. – Günter Zöchbauer Feb 27 '17 at 06:51