A tour of JavaScript Engine and why you should write JS code that looks like statically typed

  1. The JS code that we write gets executed by the JS engine in the browser and every browser has its own engine namely

2. Node by default has v8 engine but there are packages, by which you can use chakra engine with node. Generally for IOT devices, we sacrifice performance of JS engine owing to small memory capacity and use smaller engines like DuckTape or JerryScript

3. We know JS is dynamically typed, that means we do not need to tell in advance when we define var x; what type x variable is, we can either store integer or string or Boolean in it, on the other hand in C++, we need to define int x; beforehand so that the compiler knows the type of variable. This object type determination is an overhead that JS compiler has and C++ compiler does not

4. For the following code

JS engine will do JIT compilation and execute the code, but JS engine has optimizing compiler module in it, which tries to produce optimized machine level code for hot functions i.e. functions which are run many times

5. Thus along with JIT compilation, JS engine also creates optimized machine code and next time it encounter this hot function it will not do JIT compilation, but use the optimized code already created

6. But here is a caveat, this optimized machine level code is dependent of input arguments to the function, so if the same type of object is passed to the hot function, then only optimized code can be used otherwise JS engine has to again compile the code JIT

7. What is same type of object for compiler

compiler’s view of object similarity

8.So the advice for JavaScript developers would be write a code that looks like statically typed

Reference: JavaScript Engines Overview…. How do they work?