JavaScript Engine and how it works.

GURVINDER MAAN
2 min readApr 3, 2021

--

  • Js engine is computer program that executes js code. Every browser has it’s own js engine. but most powerful engine is Google’s V8 engine, V8 engine powers google chrome but also node.js which is javaScript runtime.
  • Any javaScript engine contains a call stack and a heap, call stack is where our code is executed using execution context. Heap is unstructured memory pool which stores all the objects that our memory needs.
  • in Java Script engine when a piece of code is entered .. it is first parsed or read into a data structure called AST Abstract Syntax tree. this works by splitting each line of code into pieces that are meaningful to language and then saving these pieces into tree or structured way.
  • This step also checks if there are any syntax errors and resulting tree will later be used to generate machine code.
  • This step also checks if there are any syntax errors and resulting tree will later be used to generate machine code.
  • The next step is compilation: which takes the generated AST and compiles into machine code. This machine code gets executed immediately because modern javaScript engine uses just-in-time (JIT) compilation. and execution happens in engines Call Stack.
  • Modern javaScript engines actually have pretty clever optimisation strategies. what they do is create unoptimised version of machine code in the beginning. so that it can start executing as fast as possible.
  • Then in the background code is being optimised and recompiled during the already running process. and this can be done most of times and after each optimisation the unoptimised code is simply swept away for new optimised code without ever stopping the execution.
  • This is process which makes modern JavaScript engines such as V8 of Google so fast. and all this parsing compilation and optimisation happens in the special threads inside the engine that we can not access from our code.

--

--