Magic behind the JavaScript engines Part-1 Introduction to JIT
First time when JavaScript was introduced in 1995 by Brendan Eich it was just confined just to web browsers. Netscape was the first browser with GUI accessible to normal user. So netscape team along with Brendan Eich developed the first JavaScript engine know called as SpiderMonkey. And this SpiderMonkey engine is still used in Firefox and many other browser.
So before getting into this JavaScript engine let use understand the basics of language processor.
Language processor is a system software which converts the programs to machine code for execution. Mainly there are three types of language processors:- Compilers, Interpreters and assemblers.
Compilers: They converts the higher level programs to machine level program. And is generates the machine level code for the entire program. Only then the machine level program is executed.
Interpreters: They converts the high level programs to intermediate codes and this intermediate code is translated to machine level code line by line as they execute. Its not like compilers which converts the program into byte code Ahead Of Time and then run it but it does it on the fly.
Assembler: They are some what similar to compilers but the major difference is that assembler converts the assembly code to machine level code but not the higher level code to machine code like compilers.
Now with this knowledge let us dive into JavaScript engines. The old versions of SpiderMonkey engine was similar to Interpreters. But the newer engines like V8 developed by Google’s Chromium project was way faster that the SpiderMonkey. The secret behind V8 was use of JIT (Just in time compiler). JIT is a hybrid stuff which takes best of both Compilers and Interpreters. This made a revolution in JS history and started gaining lot of popularity.
JIT works like Interpreter in running JS code line by line. And at the same time it uses the profiling data ( in simple terms its the information which helps to fine tune the code conversion ) obtained from the executing the byte code to indentify and optimize the hotspots in JS code.
Hot spots are the codes which are executed multiple times. One of the disadvantage of Interpreters is that code inside the looping statements is interpreted multiple times as if it is a new statements. But this thing is avoided in the compilers where the statements inside a loop is compiles only once. So JIT uses the profiling data which it has collected to identify the hotspots. And the code which is part of hotspots are compiled only once and used whenever required.
Based on the number of times the code is repeated the hotspots are classified into various degree of hotness. The one with lesser degree is just compiled and stored for further use, but if the code is very hot then this compiled code is sent for optimization at various levels. V8 engine use one level of code optimization but where as others engines like new versions of SpiderMonkey which are used in Firefox and Chakra engine used in MicrosoftEdge uses two level of optimization. And Safari uses JavaScriptCore uses three levels of optimization based on the hotness of the code snippet.
Because of these advancements JS which was just restricted to browsers started getting popularity in Serve Side programming, development of Native applications and even in IOT. Here is the list of few popular JavaScript engines:
Browsers: V8(Chromium),Chakra(MicrosoftEdge),SpiderMonkey(Netscape and Firefox),JavaScriptCore(Safari)
Electron: V8
NodeJS: V8,ChakraCore
IOT: DukTape,JerryScript
With these basic intuition we would coverup indepth working of JavaScript engines in upcoming articles of this series. Where we will be discuss about basic components of JS Engine, pipelining, handling of dynamic datatypes and many more.
Thank you guys,
With regards Manthan M Kulakarni
Update 7 Aug 2020
Link to second article of this series