Just-in-Time Compilation
Also known as: JIT compilation, JIT, Dynamic translation
Technique that compiles code into native machine instructions while a program is running, combining interpreter flexibility with compiled speed.
Just-in-time (JIT) compilation translates code into native machine instructions during execution rather than before it. A typical JIT system starts by interpreting a program or running a quick, unoptimized translation while monitoring which functions and loops run most often. Those hot spots are then compiled, and often recompiled with heavier optimization, into machine code for the processor at hand. The result can approach, and sometimes match, the speed of code compiled ahead of time while keeping the portability of bytecode and the flexibility of dynamic languages.
Because a JIT compiler sees the program as it actually runs, it can optimize using information unavailable ahead of time, such as which types a function really receives or which branch is almost always taken. If those assumptions later fail, the system deoptimizes and falls back to slower code. Research on languages such as Smalltalk and Self in the 1980s and early 1990s developed many of these techniques. They reached mass use through the Java virtual machine, whose HotSpot engine appeared around 2000, and through JavaScript engines such as Google's V8, released with the Chrome browser in 2008.
JIT compilation is a major reason Java, C# and JavaScript run fast enough for demanding applications, and projects such as PyPy and LuaJIT apply it to Python and Lua. The costs include slower start-up while code warms up, extra memory use and considerable engineering complexity. Generating executable code at run time also enlarges the attack surface: JIT bugs have been a recurring source of browser vulnerabilities, and some hardened browser modes disable JIT compilation entirely, accepting slower scripts in exchange for a smaller target.
Key points
- A JIT compiler turns frequently executed code into native machine code while the program runs.
- Runtime profiling lets JIT systems optimize for how code actually behaves, then deoptimize if assumptions break.
- Java's HotSpot virtual machine and JavaScript engines such as V8 brought JIT compilation into everyday use.
- Trade-offs include slower warm-up, higher memory use and a larger attack surface for security bugs.
Related entries
Compiler
Program that translates source code written in one programming language, usually a high-level one, into another form such as machine code.
Interpreter
Program that executes instructions written in a programming language directly, statement by statement, without first producing a standalone executable.
Java
Class-based, object-oriented programming language from Sun Microsystems whose programs run on the Java Virtual Machine across many platforms.
JavaScript
High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.
Python
High-level, general-purpose programming language created by Guido van Rossum, known for readable syntax and widely used in data science and AI.
Lua
Lightweight, embeddable scripting language created in Brazil in 1993, widely used to script video games and extend applications.
WebAssembly
Portable binary instruction format that lets code compiled from languages like C, C++ and Rust run safely at near-native speed in web browsers.
Attack Surface
The total set of points where an unauthorized party could try to enter, extract data from or otherwise attack a system, network or organization.