Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is AoT(or Ahead-of-Time Compilation) in Angular2?

Ahead-of-Time Compilation(or AoT) is a feature that is provided in Angular2. But I could not find good explanation on official site about it.

Could somebody make a clear definition of it?

like image 369
Stepan Suvorov Avatar asked Sep 14 '16 10:09

Stepan Suvorov


People also ask

What does AOT compilation mean?

In computer science, ahead-of-time compilation (AOT compilation) is the act of compiling an (often) higher-level programming language into an (often) lower-level language before execution of a program, usually at build-time, to reduce the amount of work needed to be performed at run time.

What is AOT and JIT compilation in angular?

JIT downloads the compiler and compiles code exactly before Displaying in the browser. AOT has already complied with the code while building your application, so it doesn't have to compile at runtime. Loading in JIT is slower than the AOT because it needs to compile your application at runtime.

What is ahead of time AOT compilation and bytecode in Java?

What Is Ahead of Time Compilation? AOT compilation is one way of improving the performance of Java programs and in particular the startup time of the JVM. The JVM executes Java bytecode and compiles frequently executed code to native code. This is called Just-in-Time (JIT) Compilation.

What is AOT and JIT in flutter?

Dart's compiler technology lets you run code in different ways: Native platform: For apps targeting mobile and desktop devices, Dart includes both a Dart VM with just-in-time (JIT) compilation and an ahead-of-time (AOT) compiler for producing machine code.


2 Answers

The template where we use angular2 peculiar syntaxes like ngFor or pipe or data binding stuffs need to be compiled to a vm friendly code, which the browser can read.

For just in time compilation (regular behaviour), the framework needs to ship the angular compiler and the template gets compiled on the browser when the app starts. This means higher bundle size angular has to ship, and longer load time, because the browser has to compile the template before it can render it.

This is analogous to how we have in browser transpilation of typescript. Because this is expensive process, we generally transpile typescript offline while bundling or build process.

Rendering template offline gives few benefits like

  • Smaller bundle size: 60% of the angular2 library is the compiler. Now that the template is compiled ahead of time, we don't need to ship the compiler anymore. This reduces the bundle size the app needs to ship
  • Faster load time: As the template is already compiled to VM friendly code, the browser takes no time in rendering the template. Results in faster page render.
like image 151
Gaurav Mukherjee Avatar answered Oct 25 '22 14:10

Gaurav Mukherjee


Angular2 docs: https://angular.io/docs/ts/latest/guide/deployment.html#!#aot

The Angular Ahead-of-Time compiler pre-compiles application components and their templates during the build process.

Apps compiled with AOT launch faster for several reasons.

Application components execute immediately, without client-side compilation.
Templates are embedded as code within their components so there is no client-side request for template files.
You don't download the Angular compiler, which is pretty big on its own.
The compiler discards unused Angular directives that a tree-shaking tool can then exclude.
like image 38
Arek - Krakiewicz.pl Avatar answered Oct 25 '22 13:10

Arek - Krakiewicz.pl