JavaScript Engine

JavaScript can run everywhere because of JavaScript Runtime Environment. It is like a container which 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 a software or a hardware, it is a program written in 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.

JavaScript can behave like both interpreted and compiled language. It simply depends on JavaScript Engine. In case of interpreter, we have speed and in case of compiler, we have efficiency. Initially, JS was a interpreted language as it majorly used to run on browser, and they didn't used 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 its 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 JS engine. 
  • Program code is first broken down into tokens. Syntax Parser takes code and convert in Abstract Syntax Tree (AST), it is simply a JavaScript object which contains tree representation of the JS source code.
  •  Execution and compilation goes 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 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 of doing it. 
  • The execution of bytecode is not possible without 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 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 their JS engine best among others, but so far at this present times, Google's  V8 engine is considered the fastest engine ever created.



For more information you can refer here.







 

Comments

Popular posts from this blog

Agile Software Development

Make Your First Pull Request!