DEV Community

Alireza Ebrahimkhani
Alireza Ebrahimkhani

Posted on • Updated on

What JIT really is ? (Part 1)

In this article i want to explain what JIT really is. Before that i want to talk about interpreters and compilers which as we can see are important pieces in our JavaScript engine.

You see in programming there are generally two ways of translating to machine language or something that our computers can understand. And what we're going to talk about here actually applies to most programming languages not just JavaScript,Python, Java, C++, any language you can think of is going to use some of these concepts.

Interpreter

Let's start with the first one, the interpreter. With an interpreter what we do is we translate and read the files line by line on the flight. Let's have a look at how this works.

I have a piece of code here, I have a function that does some calculation and we just loop over this calculation, which is five plus four right over here. And we do that a thousand times. That's a long time and you see that we get the result nine over here, but the loop is happening over and over.

Calculation code

Now with an interpreter, if I give this file to an interpreter.The translation happens line by line on the fly.And this is how you think the code should be run, right?The interpreter sees the first line and says, all right,this is a function.And then it goes to the next part and says, all right, I should loop over this a bunch of times and it starts doing some calculation.It's going to go and see.That's what the function is.I'm going to run five plus four and then I'm looping again.So I'm going to go once again, run some calculation five plus for some calculation five plus four.And it keeps running.Because interpreting code simply means taking a set of instructions like we have over here and returning an answer and doing something with that code, it's just like me telling a computer to do this, then do that, then do this.And initially, that's how JavaScript worked, it was interpreting using an interpreter, which makes sense.

Compiler

Well, a compiler like an interpreter doesn't translate on the fly. What it does is it works ahead of time to create a translation of what code we've just written. And it compiles down to usually a language that can be understood by our machines.

Let's have a look at the above code in more detail with a compiler.

Calculation code

This time around, it's going to take one pass through the code and try to understand what the code does.And it's going to take the program in JavaScript or any type of language and write a new program in your new language.
If we interpret it this language that is going line by line one at a time and running it, it's going to create the same results as our previous language, so Compiler tries to understand what we want to do and takes our language and changes it into something else and this usually happens into something called a lower level language, such as machine code.Now, I know you're still a little bit confused.
You see, the definition itself is actually a little bit fuzzy in some respects, all languages have to be interpreted and compiled because it has to run.

It has to be interpreted and it also has to most likely get translated into something low level like machine code, for example we have a high level language here like JavaScript, and we run a piece of code here an interpreter will take this code line by line and spit out bytecode that will execute code for us or a compiler might take code and go through and spit out machine code so that it can give it to a CPU so that the CPU can actually run the code.

There are two ways to run JavaScript using an interpreter or a compiler.Now, I know it's still a little bit fuzzy, but if I ask you, why would we want to do one over the other? What are the pros and cons on each? Why would you pick one over the other?

In the next part going to explore that and say what JIT really is ...

You can read the next part from here

Top comments (0)