What are AOT & JIT Compiler in Angular ?
Tags: angular, typescript, javascript
Published: 2020-02-20 05:56:05

In this article, we will cover just-in-time and ahead-of-time compilation. We will look at it in the context of an Angular project, but the principles can be applied to any front-end framework.

1. What is JIT
2. How does JIT work
3. What is AOT
4. How does AOT work
5. JIT and AOT Comparison

----

<h1>What is JIT</h1>

According to Wikipedia:

<i>In computing, <b>just-in-time (JIT)</b> compilation (also <b>dynamic translation</b> or <b>run-time compilations</b>) is a way of executing computer code that involves compilation during execution of a program — at run time — rather than prior to execution.</i>

Or stated more simply, it is that the code gets compiled when it is needed, not before runtime.

-----

<h1>How JIT works</h1>

In the beginning, a compiler was responsible for turning a high-level language into object code (machine instructions), which would then be linked into an executable.

A just-in-time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead.

-----

<h3>Flow in an Angular Project</h3>

1. Use Typescript, HTML, CSS (SCSS or SASS) to develop an Angular application.
2. Use ng build to build source code into bundles. This includes assets, JS files (modules in the case lazy load and js map, vendor, and polyfill), index.html, and CSS.
3. Then we build this into a war file to deploy by jboss or deploy directly by using heroku or another hosting that supports Node. Then we map this host to our domain by a CNAME.
4. The end-user accesses our web application via the domain. The browser will download all assets, including the HTML, CSS, and JavaScript that is needed for the default view.
5. Angular bootstraps the application
6. Angular will go through JIT compilation process for each component in the application. Then the application gets rendered.

<h4>Note</h4>

1. In JIT, not all the code is converted into machine code initially. Only code that is necessary (used immediately) will be converted into machine code. Then if a method or functionality called and is not in machine code, then that will also be turned into machine code. This reduces the burden on the CPU and makes the app render faster because it only uses what is needed.
2. You can debug in the browser during implementation because the code was compiled at JIT mode with a map file. This help you can see and link to source code directly on inspector.

----

<h1>What is AOT</h1>

According to Wikipedia:

<i>In computer science, <b>ahead-of-time compilation (AOT compilation)</b> is the act of compiling a higher-level programming language such as C or C++, or an intermediate representation such as Java bytecode or .NET Framework Common Intermediate Language (CIL) code, into a native (system-dependent) machine code so that the resulting binary file can execute natively.</i>

This seems complicated and hard to understand. He is how you can think about it:

An ahead-of-time (AOT) compiler converts your code during the build time before the browser downloads and runs that code. Compiling your application during the build process provides a faster rendering in the browser.

-----

<h1>How AOT works</h1>

1. Use Typescript, HTML, CSS (SCSS or SASS) to develop an Angular application.
2. Use ng build --prod to build source code bundles which includes assets, JS files (main, vendor, and polyfills), index.html, and CSS. In this step, Angular uses the Angular compiler to build source code and they do it in 3 phases which are code analysis, code generation, and template type checking. In this step, the bundle size will be smaller than bundle size when we build by JIT mode.
3. Then we build this into war file to deploy by jboss or deploy directly by using heroku or another hosting that support Node. Then we map this host to our domain using a CNAME.
4. The end-user accesses our web application via the domain. The browser will download all assets the including the HTML, CSS, and JavaScript that is needed for the default view.
5. Angular bootstraps and the application gets rendered.

-----

<h1>JIT and AOT Comparison</h1>

The main differences between JIT and AOT in Angular are:

    Just-in-Time (JIT), compiles your app in the browser at runtime.
    Ahead-of-Time (AOT), compiles your app at build time on the server.

JIT compilation is the default when you run the <b>ng build</b> (build only) or <b>ng serve</b> (build and serve locally) CLI commands. This is for development.

For AOT compilation, include the <b>--aot</b> option with the <b>ng build</b> or <b>ng serve</b> command. Another ways is using <b>--prod</b> which by default production mode is configured in <b>Angular.json</b> with <b>AOT</b> is set to true.

-----

<h1>Summary</h1>

JIT and AOT are two ways to compile code in an Angular project. We use JIT in development mode while AOT is for production mode.

We can easily implement features and debug in JIT mode since we have map file while AOT does not. However, The big benefit when we use AOT for production are reducing bundle size for faster rendering.