How JavaScript Engine Works?

How JavaScript Engine Works?

In this post, we will learn all about the working of the javascript engine and list popular javascript engines.

What is a javascript engine?

A JavaScript engine is a software program that executes JavaScript code and provides the relevant output.

JavaScript engines are typically developed by web browser vendors like – Google, Microsoft, and Mozilla to name a few.

How does the Javascript engine work?

The working of the javascript engine can be explained with the help if this sketch:

JavaScript Engine

 

a. Parser

The HTML parser encounters a script tag with a source. The source code inside this script gets loaded as a UTF-16 byte stream to a byte stream decoder. This byte stream decoder then decodes the bytes into a token which are sent to the parser. To save time, the engine tries to avoid parsing code that’s not necessary right away.

b. AST

The parser creates nodes based on the tokens it receives. With these nodes, the parser creates an Abstract Syntax Tree (AST).

Here you can check out how your code gets transformed to AST:

👉 https://astexplorer.net/

c. Interpreter

Next up is the interpreter, it walks through the AST and generates byte code. It reads the code line by line. When the byte code has been generated, the AST is deleted, clearing up memory space.

The problem with interpreters is that running the same code multiple times can get really slow, that’s why we have the compiler which does not repeat loops and is more optimized.

d. Profiler

The Profiler monitors and watches code to optimize it.

e. Compiler

The compiler works ahead of time and creates a translation of the code that has been written and compiles down to a lower-level language that machines can read.

Other examples of compilers are Babel, which converts modern JS to browser compatible JS, and Typescript, which is a superset of JavaScript.

f. Optimized code

Why do you need to know these things? So we can optimize the code to be run quicker.

List of javascript engines

In this post, I am listing the top 5 notable javascript engines and basic information about them.

I. V8

The V8 is the most popular javascript engine developed by Google for its Chrome browser.

Along with Google Chrome, many other Chromium-based browsers use it.

II. Chakra

Chakra is developed by Microsoft for its Internet Explorer. However, Edge was re-built using a Chromium-based browser.

So, Internet Explorer uses Chakra while Edge uses V8.

III. SpiderMonkey

SpiderMonkey is developed by Mozilla and is used in Firefox.

IV. JavaScriptCore

JavaScriptCore is Apple’s engine for its Safari browser.

V. Hermes

Hermes is developed by Facebook for Android apps using the React Native framework.

List of ECMAScript engines

An ECMAScript engine is a program that runs source code written in a variant of the standard ECMAScript language, such as JavaScript, for example.

Further Reading

If you are interested in knowing more about javascript engines then you may refer to these resources:

⭐Course Recommendations

1. Full-stack JavaScript Developer Bundle

The courses in this bundle assume no prior experience. Everything is covered from scratch.

fullstack javascript mosh

 

2. JavaScript: The Advanced Concepts

  • 24.5 hours on-demand, downloadable HD videos
  • 30+ bonus articles and resources
  • Access to exclusive ZTM community
  • Beginner, intermediate and advanced topics
  • Certificate of Completion

Advanced JavaScript Course

 

Summary

To summarize, the top 3 javascript engines used by most popular web browsers are:

  1. V8
  2. SpiderMonkey
  3. Chakra

I hope you liked the blog post. Please share it with your friends and colleagues.

Share with your friends:

Leave a Comment

Your email address will not be published. Required fields are marked *