Why JavaScript is called Interpreted or JIT(Just In Time) Compiled

Why JavaScript is called Interpreted or JIT(Just In Time) Compiled

As per MDN definition,

JavaScript is a lightweight, interpreted, or Just In Time compiled programming language.

Javascript is famous among developers for many of its advantages, features. So this series is to list out and explain each feature of this programming language.

So in the post, let’s find out why JavaScript is an interpreted, JIT(Just In Time) compiled & what does it mean?

What is Compilation

Compilation is a process of converting the program source code into machine-readable binary code, before the execution. Most of the modern program languages embrace this model to ship the application package for their execution at the end users’ machine. This helps in application performance as the code is optimized by the compiler for the end users’ platform.

But, with this model, each program needs a different compilation process for different platforms owing to changes in their underlying changes in CPU instruction sets. You might have observed when you want to install an application for your machine, you need to look for an installable specific to your OS, hardware, etc.

What is an Interpreter

An Interpreter is a program, which executes the program instructions without requiring them to be precompiled into a machine-readable format.

the interpretation approach can choose one of the below approaches:

  • parse the source code to execute the behavior,

  • translate the code into intermediate optimized representation & execute it

  • chose to execute pre-compiled bytecode(from a compiler) as well along with appropriate interpreter VM.

With the code being interpreted on the go, the execution can be initiated immediately. Since the code is compiled on the fly, it need not be built specifically to any platform, instruction & making it a great delivery experience for developers.

Since the code is not compiled, the interpreted code will not have any optimization done before the execution of the code.

JavaScript is an Interpreted, JIT Compiled

As we observed, Compilation ensures that the compiled code is optimized for faster execution & the Interpreter ensures that code execution can immediately ensure faster startup.

So, JavaScript engines are designed leveraging best of the both appro

  • aches & developed the Just In Time(JIT) Compilation model. JavaScript may be described as both compiled & interpreted language but actual implementation differs for each of the engines.

Some of the popular engines are listed below:

  • V8 from Google: Most popular one. Enables Node.js, Chrome & other chromium-based browsers.

  • SpiderMonkey : Enables Firefox & its fork implementations

  • JavaScriptCore : Enables Safari & other WebKit based browsers.

Some of the major steps in executing a Javascript is as below,

  • The Code is parsed to generate an intermediary format such as AST(Abstract Syntax Trees) which can be used for optimization.

  • The intermediary format is translated into machine-readable code by the interpreter to initiate the execution quickly.

  • The execution of the generated is monitored continuously & any code unit which has the scope for optimization is passed through the compilation step to generate the optimized code for the same.

  • Once, the optimized code is generated, its replaced in place of interpreter-generated code.

  • Thus ensuring the performance is improved gradually.

Hope it helped you understand why Javascript is called interpreted or JIT Compiled. Please share your thoughts.

Follow for more interesting posts on JavaScript & Web Development.