Just In Time Compilation of Javascript

What's Running the Web ?

In the previous writeup, while discussing the features of JS we read how Javascript is a 'Just-In-Time Compiled' language. In this section, we are going to dig deeper into the nuances of this particular feature.

What is interpretation anyway?

Interpretation languages are executed line by line. Thus, if there's an error at line 19, nothing is stopping the execution from lines 1 to 18. A typical interpretation flowchart looks as follows:

Flow Chart of Interpretation

In the early days of the language, Java-script like other scripting languages, like PERL or even Python, used to be interpreted. Interpreted languages are quite poor in performance, and as Javascript's prevalence across the web increased, the poor performance simply became unacceptable(Imagine you waiting for 2 seconds for the Amazon page to load when the Playstation 5 goes on sale ?!!!). To understand the current nature of Javascript on this parameter we need to understand the behaviour of compiled languages.

And pray to tell, What is 'Compilation'?

Well, the programming languages that are 'compiled' such as Java, C++, C etc, generally have a compiler which converts the entire code from high-level language (comprehensible to humans) to the binary sequence of 0's and 1's also called assembly language(comprehensible to your computer). Compilation creates an executable bytecode, which can then be run by the Runtime Environment at any time in the future. A typical compilation workflow looks as follows:

Compiled languages are much faster than interpreted languages. This can be appreciated by the following chart( taken from a brilliant medium article which I would highly recommend you to read):

Javascript: Parsing and Compilation

Javascript follows a two-phase process of Parsing and Compilation. The JS specification calls for "early errors", i.e. if there are errors then they should be caught the earliest possible, preferably before the code execution.

The parsing phase achieves this by just reading the code from beginning to end, to find any obvious errors like a duplicate declaration of variables. The result of an error-free code after the parsing phase is the creation of an Abstract Syntax Tree(AST). Thereafter the compiler converts the AST to an executable bytecode just before execution.

Once the execution begins, the Javascript Engine keeps on optimizing the execution in multiple cycles allowing you to load the Amazon page in milliseconds (with the bottleneck being your internet bandwidth and not javascript) and hopefully secure that elusive Playstation 5.

Conclusion

Thus Modern javascript, with its parsing and just-in-time compilation, is closer to compiled languages in spirit, if not in practice. The below image summarizes this lecture in a flowchart.

See you in the next write-up where we will read more about the Javascript Runtime Environment.

Bibliography:

  1. https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/get-started/ch1.md#you-dont-know-js-yet-get-started---2nd-edition

  2. https://medium.com/swlh/a-performance-comparison-between-c-java-and-python-df3890545f6d

  3. https://udemy.com/course/the-complete-javascript-course

Chapter1 Chapter3