JavaScript Engine

JavaScript Engine

Have you ever wondered why JavaScript can run anywhere? It can run everywhere because of JavaScript Runtime Environment. It is like a container that has everything inside it required to run a JavaScript code. JavaScript engine is the most significant part of JSRE. All browsers have their own JSRE which helps them in executing the JS code.

JavaScript Engine

It is neither software nor hardware, it is a program written in a low-level language. Some of the popular JS engines are -

  • V8 from Google.

  • SpiderMonkey is developed by Mozilla for use in Firefox and its forks.

  • JavaScriptCore is Apple's engine for its Safari browser.

  • Chakra is the engine of the Internet Explorer browser. It was also forked by Microsoft for the original Edge browser, but Edge was later rebuilt as a Chromium-based browser and thus now uses V8.

Is JS an interpreted or compiled language?

JavaScript can behave like both interpreted and compiled language. It simply depends on JavaScript Engine. In the case of an interpreter, we have speed and in the case of the compiler, we have efficiency. Initially, JS was an interpreted language as it was majorly used to run on the browser, and they didn't use to wait for the code to compile before executing. But now mostly, the modern browsers and JS engine use compiler and interpreter together, so now it entirely depends on engine whether it's purely interpreted or it is Just In Time (JIT) compiler.

All the phases of code from parsing to compilation and then to execution is carried out in the JS engine.

  • Program code is first broken down into tokens. Syntax Parser takes code and convert it into Abstract Syntax Tree (AST), it is simply a JavaScript object which contains a tree representation of the JS source code.

  • Execution and compilation go hand in hand. Interpreter makes a bytecode.

  • New optimized version of code is created by the compiler before the execution. It is in sync with the interpreter while the code is interpreted line by line, the compiler tries to optimize the code as much as it can on the runtime (that's why called JIT compilation). It can happen in multiple phases. All JS engines have their own algorithms for doing it.

  • The execution of bytecode is not possible without the memory heap and call stack of the JS engine.

  • JS uses call stack to manage execution contexts.

  • Memory heap is where all the memory is stored and is constantly in sync with a call stack, garbage collector, etc. This garbage collector continuously checks for unused variables and functions and marks them as garbage and sweeps it. This is done using different algorithms.

Every browser wants to make its JS engine the best among others, but so far at this present time, Google's V8 engine is considered the fastest engine ever created.

For more information, you can refer here.

👋 Enjoyed this blog?

Reach out in the comments below or on LinkedIn to let me know what you think of it.

For more updates, do follow me here :)

Did you find this article valuable?

Support Aakanksha Bhende by becoming a sponsor. Any amount is appreciated!