What are AOT & JIT Compiler
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.
- What is JIT
- How does JIT work
- What is AOT
- How does AOT work
- JIT and AOT Comparison
What is JIT ?
According to Wikipedia:
In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations) is a way of executing computer code that involves compilation during execution of a program — at run time — rather than prior to execution.
Or stated more simply, it is that the code gets compiled when it is needed, not before runtime.
How JIT works ?
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.
起初,编译器负责将高级语言转换为目标代码(机器指令),然后将其链接为可执行文件。
即时(JIT)编译器是运行时解释器的一个特性,它不是每次调用一个方法时都解释字节码,而是将字节码编译成运行机器的机器码指令,然后调用这个目标代码作替代。
What is AOT ?
According to Wikipedia:
In computer science, ahead-of-time compilation (AOT compilation) 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.
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.
JIT and AOT Comparison
The main differences between JIT and AOT are: (for example)
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.
Summary
JIT and AOT are two ways to compile code in an project. Generally, We can 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.