Just-in-time (JIT) compilation translates code into native machine code while a program is running, rather than translating the entire program before execution. It combines features of compilation and interpretation to improve the performance of repeatedly executed code.
How JIT compilation works
Source code is usually first translated into intermediate code, such as bytecode. At runtime, a virtual machine initially processes this intermediate code and monitors which sections are executed frequently.
Frequently executed sections, sometimes called hot code, are compiled by the JIT compiler into native machine code for the processor. The translated code can then be stored in a code cache and reused without being translated each time.
For example, suppose a loop runs 100,000 times. Interpreting its instructions on every iteration creates repeated translation overhead. A JIT compiler may identify the loop as hot, compile it once into machine code, and execute the optimized version during later iterations.
| Translation approach | When translation occurs | Main consequence |
|---|---|---|
| Ahead-of-time compilation | Before the program runs | Fast execution, but machine code is platform-specific |
| Interpretation | During execution, usually instruction by instruction | Immediate execution, but repeated translation can reduce speed |
| JIT compilation | During execution, when code is needed or frequently used | Startup overhead, followed by faster execution of compiled sections |
JIT compilation can also perform runtime optimization using information collected during execution. However, compiling code while the program runs consumes processor time and memory, so short-running programs may receive little benefit.
Exam technique
For IB Computer Science HL topic A1.4 Translation, define JIT compilation by stating both its timing and output: it translates intermediate code into native machine code at runtime. A common misconception is that JIT is simply another name for an interpreter; unlike interpretation alone, JIT stores compiled machine code so that it can be executed again directly.