Inside the Javascript Engine: Compiler and Interpreter
Is Javascript compiled or interpreted?
In Programming, there are generally two ways of translating to machine language or something that our computers can understand.
Now, this applies to most programming languages, not just javascript; Python, Java, C++ all use the same concepts.
The Interpreter
Here we read the files line by line on the fly.
Let’s take the above code as an example. Here, all we are doing is looping over our calculation (5+4) 1000 times. This is looping over and over. Our result is 9.
Now if we gave this file to an interpreter, the translation happens line by line; and normally it's how you would assume code is run.
Babel is a Javascript compiler that takes your modern JS code and returns browser compatible JS (older JS code).
Typescript is a superset of Javascript that compiles down to Javascript.
From line one (function) ->second line(return) ->third part (for loop) and -> the calculations.
Because interpreting code means taking a set of instructions and returning an answer. This is how initially javascript worked.
A Compiler
A compiler, unlike an interpreter, doesn't translate on the fly. It works ahead of time to create a translation of the code we have written. It compiles it down to a language that can be understood by the machine.
A compiler will take one pass through the code and try to understand what the code does. It will take a program in javascript and write the program in a new language x. And if we take this language x it spits out and interpreted it, we would create the same results as our language.
So a compiler will try to understand what we want to do and will turn our language into usually a low-level language such as machine code.
Now, it's a little weird when you think about it because in most cases all languages have to be interpreted and compiled. A program has to run and be interpreted, but it also has to get translated into something like machine code.
For example, consider a high-level language above like Javascript; an interpreter will read this code line by line and spit out byte code that executes this code for us. Or a compiler might take our code and spit out machine code to give to a CPU so it can run the code.
But as we said earlier most languages are both compiled and interpreted. Because our x86 code or machine code is handed to a CPU, it also needs to be interpreted. In reality, the x86 code is too slow to be interpreted literally so instead most modern CPUs will compile this code into its own native microcode.
So there are two ways to run Javascript code — using a compiler and an interpreter. But why use one over the other? What are the pros and cons of each?
Interpreters
PROS: They are easy to get up and running because we don't have to convert our code into another language like how we saw language x. There is no compilation step before you start running your code. You just give the interpreter a file and it starts translating it line by line on the fly.
In a sense, this means that an interpreter is good for a language like Javascript because when a file is sent from the server, we want it to execute right away; a user will be waiting to interact with our application. Javascript was created for the browser and so being able to interpret Javascript and run it as fast as possible was ideal. It’s why the language used interpreters at the beginning.
CONS:
When you are running the same code over and over, for example, when you are in a loop, things get really slow.
Compiler PROS
Because a compiler doesn't need to repeat the translation for each pass through in a loop, for example, the code generated is faster. These edits compilers do are called optimizations. The code from a compiler gradually gets faster and faster as more optimizations are done.
CONS
The compiler takes a little more time to start because it has to go through that compilation step at the beginning but when it sees repeated code or loops, it will simplify the code.
So compiler or interpreter?
The compiler which takes a little bit longer to get up and running but the code is eventually going to be faster or the interpreter that is easy to get up and running right away but unfortunately doesn't do any optimizations?
But what if we combine the best of both worlds?
What if instead of just using an interpreter and compiler, we combine them and create something called a JIT (Just in time compilation)compiler? This is something that browsers like Chrome started doing to make the engines faster.