Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript is compiled or interpreted language or both?

Tags:

javascript

I'm sorry for this silly question but I'm confusing about that.

I have been reading a you don't know JS yet book and the book said that

JS is most accurately portrayed as a compiled language.

And they explain with some example and that makes sense for me

But when I'm searching on the internet. Most people suppose that JS is an interpreted language. I read that JS engine uses many kinds of tricks to handling JS programs like JIT, hot-recompile.

So should I consider Javascript to be both compiled and interpreted language?

like image 210
Tu Le Thanh Avatar asked Jan 24 '23 13:01

Tu Le Thanh


1 Answers

UPDATE:

When JavaScript first came in 1995-96, Brendan Eich created the very first JS-Engine called spider-monkey (Still used in Mozilla Firefox). At this time JavaScript was created keeping Browsers in mind. So that any file coming from the Servers would be quickly interpreted and shown by the Browsers.

Interpreter was a best choice to do so, since Interpreters executes code line by line and shows the results immediately.

But as time progressed Performance became an issue, It was becoming slower and slower. The problem with Interpreters is that when you are running the same code over and over again in a loop like this one:

const someCalculation = (num1, num2) => {
  return num1 + num2;
};

for (let i = 0; i < 10000; i++) {
  someCalculation(5, 6); // 11
}

It can get really really slow.

So the best option was introducing the Compiler,

which actually helps us here. It takes a little bit more time to start up, because it has to go through the compilation step at the beginning - Go through our code, understand it and spit it out into another language. But the Compiler would be smart enough. When it sees the code like above ( where we loop over and it has the same inputs , returning the same outputs), it can actually just simplify this code and instead of calling this function multiple times it can just replace this function with output for the function. Something like this.

const someCalculation = (num1, num2) => {
  return num1 + num2;
};

for (let i = 0; i < 10000; i++) {
  11; // And it will  not call someCalculation again and again.
}

Because the Compiler does not repeat the translation for each pass through in that loop, the code generated from it is actually faster.

And these sorts of edits that Compilers do are called Optimizations

Thus Javascript combined both Interpreter and Compiler to get the best of both the world. So Browsers started mixing compilers called JIT-Compilers for just-in-time compilations to make the engines faster.

V8-Engine

In the Image you can see a Profiler which keeps a watch on the repeated code and passes it on to the Compiler for Code Optimizations.

This means that the Execution Speed of Javascript Code that we entered into the engine is going to gradually improve because the Profiler and Compiler are constantly making updates and changes to our Byte code in order to be as efficient as possible. So Interpreter allows us to run the code right away while Profiler and Compiler allows us to optimize this code as we are running.

Now Let's come to some conclusions:

Now that we know how JS-Engine works underneath the hood, we as Programmers can write more Optimized Code - code that the Compiler can take and run it faster than our regular Javascript. However,

We need to make sure we don't confuse this Compiler- because the Compiler is not perfect, it can make mistakes and it may try to optimize the code that exactly does the opposite. And if it makes a mistake and it does something unexpected, it does something called De-Optimization which takes even more longer time to revert it back to the interpreter.

NOW THE BIG QUESTIONS : Is Javascript an interpreted language?

ANSWER : Yes, initially when Javascript first came out, you had a Javascript engine such as spider-monkey- created by Brenden Eich that interpretted javascript to Byte Code and this Javascript engine was able to run inside of our browser to tell our Computers what to do.

But things have evolved now, we don't just have interpreters, we also use compilers to optimize our code. So, this is a common misconception.

When someone says Javascript is an interpreted language, yes there is some truth to it but it depends on the implementation. You can make an implementation of Javascript Engine that perhaps only compiles. Technically it all matters depending on the implementation.

like image 88
Imran Rafiq Rather Avatar answered Feb 04 '23 19:02

Imran Rafiq Rather