JavaScript is not directly understood by computers but the browsers have an inbuilt JavaScript engine which helps to convert our JavaScript program into computer-understandable language. So whenever someone talks about running code they are talking about a javascript engine.
Javascript is a dynamic language so we expect it to be slow but it is pretty fast the reason is all the modern runtimes use something called JIT(just in time compile) because of that it is pretty fast.
Here is a list of the javascript engines in some popular browsers:
- V8 (Chrome)
- Chakra (Internet Explorer)
- Spider Monkey (Firefox)
- Javascript Core Webkit (Safari)
As an example, I will explain the workflow inside of the V8 engine:
for optimizing they use two compilers
- baseline compiler ( V8 uses Ignition): it is regular compiler that generates machine code.
- optimizing compiler (V8 uses turbofan): saves the type information for "hot" functions
Since javascript is a dynamic typing language we don't know the types when a user enters. so the compiler needs to find the types each time.
For optimization, we use an optimizing compiler which does:
Re-compile: (if it is hot or used a lot) "hot" functions with type information from previous execution
De-optimize: since javascript dynamically typed language, when the type changes remove it.
If we don't change the types optimizing compiler can remember the types for the same functions so the code can be faster
so if we let the optimizing compiler do its job and don't change the types our code will be much faster.
Top comments (0)