Parsing and Executing JavaScript Code

Tushar KaushikTushar Kaushik
3 min read

The first JavaScript engine was developed by Brenden Eich (as you already know him as the founder of JavaScript) which later got evolved into what we call today as spider monkey engine [Mozilla Firefox Engine].

JS Engine - software component/program that executes the JavaScript code.

First JavaScript engine used to execute JavaScript code as an interpreter but all modern engines uses just-in-time (JIT) compilation to improve performance, now you may ask what are these terms interpreter and JIT compilation .so, there are different techniques to execute the code .

Interpreter translates the source code line by line into low-level language and then immediately executes each translated line.

Compiler translates the source code into low-level language all at once and only then that low-level language code or translated code is executed.

JIT compilation combines the speed of compiler with the flexibility of interpreter. Basically ,it uses good parts of both compiler and interpreter.

Code > Parsing > Compilation > Execution

Firstly all code enters in parsing phase in which Lexer tokenises all code then that tokenised code is converted into Abstract Syntax Tree (AST) by syntax parser then comes the compilation phase in which JIT Compiler will convert AST into byte code which further will be converted into machine mode then that machine code is executed.

During compilation or JIT compilation phase many techniques are used to optimise the code viz., inlining ,copy elision and inline caching.

  • Inlining is an optimisation technique in which compiler replaces function calls with the actual function code and eliminates overhead of function calls and returns.

  • Copy elision is an optimisation technique in which compiler avoids unnecessary copies of objects during function calls and uses move semantics to transfer ownership instead of copying.

  • Inline caching is an optimisation technique in which compiler caches the result of a function call along with its arguments and uses that cached result instead of calling the function again which eventually reduces the number of function calls.

Call Stack is crucial in JavaScript engines that tracks function calls and contexts .It maintains the order of execution of execution context and where the control is .It follows a Last-In-First-Out (LIFO) principle, where the most recently added function call is the first to be removed. When a function is called, the engine pushes that call in the call stack.

Garbage collector in JavaScript engine clears unused or irrelevant memory from memory heap .It is an essential component of JavaScript engines that automatically manages memory allocation and deallocation. It identifies and removes objects that are no longer needed by the program, freeing up memory resources. This process runs periodically in the background, ensuring efficient memory usage and preventing memory leaks .It works on mark and sweep algorithm.

The mark sweep algorithm is one of the common techniques used by garbage collectors to determine which objects are still in use and which can be safely removed. Mark and Sweep Algorithm has two phases :

  • Marking

  • Sweeping

Firstly, it starts from the root objects (like global variables) and traverses through all reachable objects, marking them in use .after that it scans the entire heap, freeing memory occupied by unmarked objects. This process ensures that only objects that are not in use are removed, while preserving those which are still in use by the program.

0
Subscribe to my newsletter

Read articles from Tushar Kaushik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Kaushik
Tushar Kaushik