JIT
Just-In-Time Compilation
Compiles bytecode to machine code at runtime. Used in the JVM and V8.
Just-In-Time Compilation
Compiles bytecode to machine code at runtime. Used in the JVM and V8.
JIT (Just-In-Time) compilation is a technology that converts bytecode into machine code while a program is running. It is a hybrid approach that strikes a balance between the slow execution speed of a pure interpreter and the long compile times of an AOT (Ahead-of-Time) compiler, and it is a core technology of modern programming language runtimes.
A JIT compiler uses a strategy called "hotspot" detection. When a program first runs, an interpreter interprets the bytecode while profiling frequently called functions and repeatedly executed loops. Once a certain threshold is exceeded, that code is compiled to machine code so later calls run at native speed.
The biggest advantage of JIT is adaptive optimization using runtime information. By analyzing type information, branch prediction data, and inlining candidates collected during execution, it can perform more aggressive optimizations than an AOT compiler. For example, V8's TurboFan infers JavaScript's dynamic types at runtime and achieves performance comparable to statically typed languages.
In practice, JIT is used in major language runtimes such as Java's HotSpot JVM, JavaScript's V8/SpiderMonkey, Python's PyPy, and .NET's RyuJIT. In microservice and serverless environments, warm-up time becomes an issue, so there is a trend toward using it together with AOT/snapshot technologies such as GraalVM Native Image and Java's CRaC.
// Java: JIT 컴파일 최적화 관찰하기
public class JITDemo {
// JIT이 인라이닝하기 좋은 작은 메서드
private static int add(int a, int b) {
return a + b;
}
// 핫스팟이 될 루프 - JIT 컴파일 대상
public static long hotLoop(int iterations) {
long sum = 0;
for (int i = 0; i < iterations; i++) {
sum = add((int) sum, i); // 반복 호출로 인라이닝 대상
}
return sum;
}
public static void main(String[] args) {
// 워밍업: JIT 컴파일 유도
for (int i = 0; i < 10000; i++) {
hotLoop(1000);
}
// 벤치마크: JIT 최적화된 상태
long start = System.nanoTime();
long result = hotLoop(100_000_000);
long elapsed = System.nanoTime() - start;
System.out.printf("결과: %d, 소요시간: %.2fms%n",
result, elapsed / 1_000_000.0);
}
}
// 실행 옵션으로 JIT 동작 확인:
// java -XX:+PrintCompilation JITDemo
// java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining JITDemo
"The high cold-start latency in serverless environments comes from JIT warm-up. We should consider AOT compilation with GraalVM Native Image, or pre-warming instances with Provisioned Concurrency."
"A JIT compiler combines the strengths of an interpreter and a compiler. It starts quickly with the interpreter, then finds hotspots through profiling and compiles them to machine code. The V8 engine optimizes JavaScript by combining the Ignition interpreter with the TurboFan JIT compiler."
"This benchmark code has no warm-up phase. Performance differs greatly before and after JIT compilation, so you need to add at least about 10,000 warm-up iterations before measuring to get results close to real production performance."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
If you run a benchmark before JIT compilation completes, you end up measuring interpreter performance. Always measure after sufficient warm-up iterations. Using a benchmark framework such as JMH is recommended.
JIT optimizes code with consistent types better. In JavaScript, assigning multiple types to the same variable can cause V8 to deoptimize, and performance can drop sharply.
Use consistent types, small functions (inlining candidates), and predictable branch patterns. Avoiding reflection and dynamic dispatch on hot paths maximizes the benefit of JIT optimization.