What is AOT and JIT Compiler in Angular ?
Last Updated : 05 Nov, 2020
An angular application mainly consists of HTML templates, their components which include various TypeScript files. There are some unit testing and configuration file. Whenever we run over an application, the browser cannot understand the code directly hence we have to compile our code.
What is Ahead of Time (AOT) compiler ?
All technologies Ahead of Time is a process of compiling higher-level language or intermediate language into a native machine code, which is system dependent.
In simple words, when you serve/build your angular application, the Ahead of Time compiler converts your code during the build time before your browser downloads and runs that code. From Angular 9, by default compiling option is set to true for ahead of time compiler.
Why should you use the Ahead of Time compiler ?
- When you are using Ahead of Time Compiler, compilation only happens once, while you build your project.
- We don’t have to ship the HTML templates and the Angular compiler whenever we enter a new component.
- It can minimize the size of your application.
- The browser does not need to compile the code in run time, it can directly render the application immediately, without waiting to compile the app first so, it provides quicker component rendering.
- The Ahead of time compiler detects template error earlier. It detects and reports template binding errors during the build steps before users can see them.
- AOT provides better security. It compiles HTML components and templates into JavaScript files long before they are served into the client display. So, there are no templates to read and no risky client-side HTML or JavaScript evaluation. This will reduce the chances of injections attacks.
How Ahead of Time works ?
We use Typescript, HTML, style-sheets to develop our Angular project and ng build –prod or ng build to build our source code into bundles which include JS files, index.html, style-sheets, and assets files.
Now Angular uses the angular compiler (whichever you have selected) to build source code, and they do it in three phases, which are code analysis, code generation and template type checking. At the end of this process, bundle size will be much smaller than the JIT compiler’s bundle size.
After that AOT builds this into a war file to deploy directly by using Heroku or by JBoss or by any other hosting that supports Node. And then we map this host to the domain by using a CNAME.
Now the clients can access your web application via the domain. The browser will download all necessary files like HTML, style-sheets, and JavaScript which is needed for the default view. At last, your application will get bootstrap and get rendered.
How to compile your app in ahead of time compiler: For compiling your app in Ahead of time, you don’t have to do much, because from angular 9 default compiling option is set to Ahead of time. Just add –AoT at the end ng serve –aot.
What is the Just in Time (JIT) compiler ?
Just in time compiler provides compilation during the execution of the program at a run time before execution. In simple words, code get compiles when it’s needed, not at the build time.
Why and When Should you use Just In Time Compiler ?
- Just in time compiler compiles each file separately and it’s mostly compiled in the browser. You don’t have to build your project again after changing your code.
- Most compiling is done on the browser side, so it will take less compiling time.
- If you have a big project or a situation where some of your components don’t come in use most of the time then you should use the Just in time compiler.
- Just in Time compiler is best when your application is in local development.
How Just in Time compiler Works?
Initially, compiler was responsible for converting a high-level language into machine language, which would then be converted into executable code.
Just in time compiler, compiles code at runtime which means instead of interpreting byte code at build time, it will compile byte code when that component is called.
A few important points:
- In case of Just in time, not all code is compiled at the initial time. Only necessary component which are going to be needed at the starting of your application will be compiled. Then if the functionality is need in your project and it’s not in compiled code, that function or component will be compiled.
- This process will help to reduce the burden on the CPU and make your app render fast.
- One more interesting thing is, you can see and link to your source code in inspect mode because Just in Time, compiles your code with JIT mode and a map file.
Comparison between Ahead of Time (AOT) and Just in Time (JIT) –
JIT | AOT |
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. | Loading in AOT is much quicker than the JIT because it already has compiled your code at build time. |
JIT is more suitable for development mode. | AOT is much suitable in the case of Production mode. |
Bundle size is higher compare to AOT. | Bundle size optimized in AOT, in results AOT bundle size is half the size of JIT bundles. |
You can run your app in JIT with this command: ng build OR ng serve | To run your app in AOT you have to provide –aot at the end like: ng build --aot OR ng serve --aot |
You can catch template binding error at display time. | You can catch the template error at building your application. |
Conclusion: You can compile your angular application in two ways: JIT and AOT. Both are suitable for a different scenario like you can use JIT for development mode and AOT is better in production mode. Implementing features and debugging is easy in JIT mode since you have to map files while AOT does not have it. However, that AOT provides a big benefit to angular developers for production mode by reducing bundle size and making your app render faster.
Similar Reads
What is the AppModule in Angular ?
In Angular, AppModule plays an important role as the entry point to an Angular application. In this article, we'll learn about what AppModule is, its structure, and its significance in Angular applications. We'll also look at some examples to have a clear understanding. Table of Content What is AppM
4 min read
What is CommonModule in Angular 10 ?
In this article, we are going to see what is CommonModule in Angular 10 and how to use it. CommonModule is used to export all the basic Angular directives and pipes. It is re-exported when we import BrowserModule into our angular application, BrowserModule is automatically imported into our applicat
2 min read
What is a custom directive in Angular?
Angular, a popular framework for building dynamic web applications, offers a powerful feature known as custom directives. These directives extend the functionality of HTML elements, enabling to create reusable components and add behavior to their applications. In this article, we'll learn about the
4 min read
What is the factory function in Angular ?
In Angular, the factory function is always inclined towards class and constructors. Generally, the factory function is used to provide service as a dependency in any angular application. A factory function generates an object, provides it with some logic, executes the function, and returns the objec
4 min read
Difference between link and compile in AngularJS
In this article, we will see the link and compile features in Angular JS, along with understanding their implementation through the illustration and exploring the key differences between them. One of the fundamental components of AngularJS is the directive. When creating web components, a directive
5 min read
What is entryComponents in angular ngModule ?
The entryComponent is the component which loads angular by force, that means these components are not referenced in the HTML template. In most of the cases, Angular loads a component when it is explicitly declared in the component template. But this is not the case with entryComponents. The entryCom
3 min read
What is the use of Bootstrap Datepicker in Angular?
In this article, we will see the use of Bootstrap Datepicker in Angular. The Datepicker is used to make a component that will be shown by using a calendar and we can select the date from it. Adding Bootstrap to the Angular Project can make the better UI design, along with providing some in-built cla
3 min read
What is Angular Expression ?
Angular is a great, reusable UI (User Interface) library for developers that help in building attractive, and steady web pages and web application. In this article, we will learn about Angular expression. Table of Content Angular ExpressionDifferent Use Cases of Angular ExpressionsSyntaxApproach Ang
4 min read
What is the Difference between Constructor and ngOnInit in AngularJS ?
Constructor: Constructor is the default method for a class that is created when a class is installed and ensures the proper execution of the roles in the class and its subsections. Angular are preferably the Dependency Injector (DI), analyzes the builder's components and when creating a new feature
3 min read
What is NgStyle in Angular 10 ?
In this article, we are going to see what is NgStyle in Angular 10 and how to use it. NgStyle is used to add some style to an HTML element Syntax: <element [ngStyle] = "typescript_property"> Approach:Â Create the Angular app to be usedIn app.component.html make an element and sets its class us
1 min read