How did JavaScript go from a slow toy to a fast language?
A language written in ten days for small tricks on web pages now gets compiled to machine code while you watch it run.
▶ Start the storyJavaScript got fast because browsers learned to compile it while it runs, a trick called , or JIT. Compilation means translating code into the a processor runs directly. JIT compilation does it during execution of a program at run time rather than before execution. In 2008 Google debuted its Chrome browser with the V8 , which was faster than its competition. The key innovation was JIT, so other browser vendors needed to overhaul their engines for JIT.
The language did not start with that ambition. In the Web's early years, pages could only be static, lacking the capability for dynamic behavior once the page was loaded. In 1995 Netscape decided to add a programming language to its browser, a "language for the masses" to help nonprogrammers create dynamic, interactive web sites. Brendan Eich completed the first version in ten days to meet the schedule of the Navigator 2.0 Beta. It first shipped as LiveScript, with an implementation, and was renamed JavaScript for the official release in December 1995.
Why does JIT help? Interpreting and compiling each have strengths, and JIT compilation combines the speed of compiled code with the flexibility of interpretation, at the cost of the overhead of an plus the overhead of compiling. A JIT system tries to get the best of both: it continuously analyses the code being executed and identifies the parts where the speedup gained from compiling would outweigh the overhead of compiling them. Sun's HotSpot Java works this way: the code is initially interpreted, but it monitors which sequences of are executed often and translates them to .
Interpreter
- Starts right away
- Runs the program as it goes
- Slower on busy code
JIT compiler
- Watches the running code
- Compiles the hot parts to
- Spends time only where it pays off
V8 took the same road. Much of its development is strongly inspired by HotSpot. It builds with an called Ignition, and TurboFan compiles that bytecode into , optimizing and re-optimizing it dynamically as it runs. A language written in ten days thus ended up on an engine that compiles it, and keeps re-optimizing it, while it runs.
Quiz me
0/3
Recap
Interpret first, watch what runs often, and compile only that part while the program runs.
💡 A trick to remember it · A JIT engine is a cook who only writes down the recipe steps they keep repeating.
Surprising fact · JavaScript was first completed in ten days, and its speed came from ideas that grew out of Self and Java's HotSpot.
Sources (5)
No source, no claim. Every fact in this lesson (16 claims) cites at least one of these.