We hear about Javascript as it is an interpreted language and the JIT engine made up of it. But did you know that Javascript is also considered either a compiled or an interpreted language thanks to the JIT engine?
What is JIT?
JIT stands for Just-in-time, it is a dynamic compilation which is being done before the code execution. In the normal process of translate Javascript into machine readable code, the interpreter translates the human code (Javascript) line by line and execute that code (bytecode - 0 and 1) immediately, so there's no room for optimization, just translate and run. The JIT leans in this process to do the optimization between the translation and execution.
How JIT helps
The JIT engine plays a vital role in the optimization. It will do assumptions and estimations in parts of our code, then decide what code can be sent directly to execution and which one should be hold to optimize.
The JIT holds some assumptions for our code when it's doing the optimization. With some code is run frequently, the engine will capture the code, make assumption and optimized version for each of its assumption. If the assumption is correct, the optimized code will be pushed to the compiled code and ready for execution, if it's not, the wrongly-optimized code will be trashed out - this is called deoptimization.
Elaborate more on how JIT works - A crash course in just-in-time (JIT) compilers.
The process of op-deop may cause the engine to slow down the run time of the code due to making to many assumptions by the JIT. The browsers solve this problem by limiting how many time the op-deop should occur, if it reaches the limit, the op-deop should be stopped trying and use the baseline code.
Conclusion
We studied what is JIT and how it helps transform Javascript in to a well-balanced between compiled and interpreted language. Each browser or server engine has different ways of implementing JIT but in general, they have the same core idea which includes the op/deoptimization with the baseline compiler and optimization compiler.
References:
Top comments (0)