DEV Community

Syed Ashar Ali
Syed Ashar Ali

Posted on

Why JIT was introduced?

Image description
As we know that there are two ways of translating in programming which is "Compiler" and "Interpreter". There are pros and cons in each of these ways of handling the translation.ย 

Interpreter as compared to Compiler, has quickly get up and running ability which makes it faster. But the JavaScript Interpreter trade-off was to interpret the code over and over again in case of looping.

To get the best of both worlds, browser mixed the compilers in and added a new part in JS engine named as "๐ฆ๐จ๐ง๐ข๐ญ๐จ๐ซ" or "๐ฉ๐ซ๐จ๐Ÿ๐ข๐ฅ๐ž๐ซ".
What monitor does is that it watches the code entirely as it runs. It keep tracks of things like how often the function has been executed. In case of repeated functions, that function is considered as "๐ฐ๐š๐ซ๐ฆ๐ฌ". That warmed function then get it off the "๐›๐š๐ฌ๐ž๐ฅ๐ข๐ง๐ž ๐œ๐จ๐ฆ๐ฉ๐ข๐ฅ๐ž๐ซ" to create compiled version of it.

The baseline compiler will do it in chunks. Each operation in the function is compiled into one of more "๐ฌ๐ญ๐ฎ๐›๐›๐ฌ". Stubb will be specific to whatever types are being used to either sides of that operator and stored. In case of repeated operation with same operator and same type of either side of operator, the stored stubb will be used instead of reading the block of code again. This will save in translation time and help speed things up.

As one of the characteristics of compiler is that it takes time to think the best way to communicate with the machine which we referred as "optimization". The baseline compiler will make some optimizations but it does not wants to take up much of the time as the code is executing as the same time. If the code is very hot, it will take worthwhile in optimization.

In case of a very hot function, the monitor send that function to "๐จ๐ฉ๐ญ๐ข๐ฆ๐ขz๐ข๐ง๐  ๐œ๐จ๐ฆ๐ฉ๐ข๐ฅ๐ž๐ซ" and this will create the faster version of that function. The optimizing compiler makes assumptions. For example: if the optimizing compiler assumes that all of the objects that are created by a particular constructor had a same shape, so the object has the same property names and they have been added in the same order. So the optimizing compiler uses the information that the monitor has been gathering to make such judgements and if something has been true in the past of code, it continues to consider it to be true.

The compiled code needs to be validated and if it's not true, the JIT assumed it as wrong assumption and dumped the optimized code. At this point, it goes back to the compiled version and do "๐ซ๐ž-๐จ๐ฉ๐ญ๐ข๐ฆ๐ข๐ณ๐ข๐ง๐ ".

There could be a possibility of running cycle between the optimizing and re-optimizing in case of wrong assumption, so JIT keep tracks of how many times they have optimized a function, and it's not working out then it will mark it to not to optimize the function and move on.

This is JIT in nutshell. Thankyou for reading it out till the end. This is my first blog and I'll be looking for more blogs in future.

Top comments (0)