JavaScript Under The Hood [5] - JavaScript Engine Overview

Traversy Media · Beginner ·🏗️ Systems Design & Architecture ·3y ago

Key Takeaways

This video series covers the basics of JavaScript engines, including the V8 engine, SpiderMonkey, Chakra, and JavaScript Core, and explains how JavaScript code is interpreted and executed, including just-in-time compilation and abstract syntax trees.

Full Transcript

what's going on guys welcome to part five of JavaScript under the hood so in this video we're gonna have a look at JavaScript engines now we've talked about things like the threat of execution and the call stack and and all of this does happen within the JavaScript engine however what I want to focus on in this video is what happens when your source code is actually interpreted and executed what are the stages that it goes through to get to being machine code that runs on the CPU okay and we'll also look at the different engines that each browser uses all right so let's go ahead and get into it foreign guys so all browsers have a JavaScript engine which essentially is a software component in the JavaScript runtime environment that interprets optimizes and executes JavaScript code now the first engines were just mere interpreters but modern engines are much more intricate and do a lot more optimization including something called jit or just-in-time compilation to increase performance and I'll talk more about this later now most engines are written in languages like C and C plus plus different browsers use different engines they all essentially pretty much do the same thing at a high level but they do differ in their approach and I'll mostly be talking about the V8 engine that Google Chrome uses and that node.js uses when I talk about the inner workings so let's just quickly look at the different engines that each browser uses so Mozilla Firefox uses spider monkey which was actually the the very first JavaScript engine created and it was created by the creator of JavaScript itself Brendan Ike or Brendan ack I'm not exactly sure how to pronounce it but it was used in the Netscape Navigator browser for those of you that are old enough to remember that as I am and then later on it was open sourced and is now is currently maintained by the Mozilla foundation and then the V8 engine is what Google Chrome uses as well as node.js it's extremely fast I believe it's the fastest JavaScript engine don't quote me on that but I'm pretty sure I've heard that multiple times it can be it can run Standalone or it can be embedded into a c plus application and of course it's used with node.js which is a JavaScript runtime that lets us run JavaScript on the server next we have chakra which is what Microsoft Edge uses and Internet Explorer also used an engine called chakra but it was actually a jscript engine jscript was Microsoft's Legacy dialect of the ecmascript standard so it's basically like Microsoft's version of JavaScript one unique feature of chakra is that it compiles scripts on a separate CPU core parallel to the web browser and then Safari uses something called JavaScript core along with a bunch of other Apple applications and Safari you also uses a rendering engine called webkit and all webkit browsers use the JavaScript core engine it provides the ability to evaluate JavaScript programs from within Swift Objective C and C based apps all right so those are the major browsers and the engines that they use now before we look at the inner workings of the JavaScript engine I just want to talk about compiling versus interpreting I'll compile languages versus interpreted languages so JavaScript is what we call and interpret the language and then languages like C and C plus plus these are compile languages now compile languages means you you write the code you run the code through a compiler and it and it turns it into something called machine code and this is usually represented with ones and zeros and a computer or I should say a CPU doesn't directly understand JavaScript or even C or C plus plus it only understands machine code now since languages like C and C plus plus are directly compiled to machine code they're extremely fast and Powerful you know they they do have that extra step of compilation but that only happens once and then your compile code is executed from there on all right now with an interpreted language like JavaScript or python or Ruby the code is not directly compiled into machine code at least not all at once so you don't have that extra step Instead The Interpreter reads your code line line by line checking for errors and runs the program simultaneously so it interprets statements into machine code and statements include source code pre-compile code and scripts so at the end of the day we always end up with machine code but the way we get there is different now the analogy that I I thought of and I think makes sense at least to me is if you were in a foreign country and you needed to have a conversation with someone you know that spoke a different language compiling is like stopping and taking the time to learn the entire language and interpreting would be like having someone there to interpret each word or each sentence so compiling does take long take longer in the beginning obviously to learn the language but then you know the entire language so you have a fast and smooth conversation where interpreting doesn't take all that time to compile but the conversation is going to be slower because you have to interpret every word or every sentence all right so compile language they take longer to create something which is called Write time but they have an extremely fast run time where interpreted languages have a short right time um you know you can write and test your code quicker but they have a slower run time they're typically not as powerful so I know that was kind of an aside but I think it's good to know the the general difference between compile languages and interpreted languages so what I want to do now is show you the process of getting our JavaScript source code you know all of our functions and arrays and objects and variables get that to machine code which is just ones and zeros that can run on the CPU now at one point JavaScript engines were just simple interpreters basically like just this step here or these couple steps but over time they've become more intricate and they can optimize code better and they're a lot faster than they used to be and of course every JavaScript engine works in a different way this is really my understanding on the V8 engine which is the engine that I've studied the most and it depends on what time frame you're talking about as well because the V8 engine worked in a different way just a couple of years ago all right so keep that in mind but basically we have our source code that we write and then the first step is it's going to go into the parser and the parcel will check your source code which is your your JavaScript code and it will go through line by line and make sure that the syntax is correct if it comes across an error it stops running and sends out that error now if the code is valid it generates something called an apps an abstract syntax tree or AST which is a tree of nodes that represent the code and each node of the tree denotes a construct occurring in the text and you can almost think of it as like like how the JavaScript Dom is the document object model now the AST doesn't include things like parentheses curly braces quotes it just represents the code as a tree of nodes the AST is also not unique only to JavaScript it's used in many other languages as well now there's actually a website called AST explorer that allows you to input some code it can be JavaScript as well as many other languages and it will show you what the actual AST looks like so I just found this the other day and I thought it was pretty cool so let's just go ahead can I make this bigger let's let's do that so we'll say const x equals 100 and as you can see over here we now have a variable declaration it has a type the start and and then in declarations if we uncollapse this we should be able to see the value yep so in this init right here you'll see the value 100 and the identifier has a name of X so this is what it's going to look like when your code is is turned into this abstract syntax tree and then if I were to add let's say a function we'll call it add 10 and we'll pass in X and then let's just do a return and we'll just say we want to return X plus 10 and we'll close that up so now if we look over here let's just collapse the uh yeah so now you see the variable declaration underneath that we now have a function declaration and I'm not going to pretend I know exactly how to read this but you can see the identifier has a name of add 10 we have the params so the param of X and then in the body we have a return statement we have an argument a binary expression and then the left identifier is going to be the X so name X and then the right is going to be the value of 10. so just to give you an idea of you know what happens behind the scenes so now let's go back to my little diagram here and once we have that AST next we have The Interpreter whose job it is to take that AST and transform it into something called byte code which is an IR or intermediate representation so you can think of it as an abstraction of machine code or small building blocks that make up any JavaScript functionality when you compose them together again you don't have to fully understand the stuff I'm just giving you a high level overview now you may ask why doesn't The Interpreter compile directly to machine code well one reason is because machine code is unique to the architecture of your machine so machine code for Intel based processors is going to be different than machine code for arm processors and byte code or any intermediate representation is universal another reason is because we can run optimize stations on byte code now most engines use something called jit compilation or just in time compilation which will take our byte code and transform it into machine code on the Fly and a jit compiler helps optimize your code because it has access to certain Dynamic runtime information so the spider monkey engine uses a jit compiler called ion monkey and the V8 engine uses I think it's called turbofan and like I said earlier with a compile language like C your entire code is compiled before execution that's called aot or ahead of time compilation where just in time compilation refers to compilation at run time and again it allows for optimizations all right and then finally the machine code is then run by the machine's Hardware alright so I know that that's a lot to take in technically you don't need to know all this stuff up for practical web development but I think it can help to just understand what happens under the hood and some of this stuff can also be asked in interview questions for uh for Dev jobs just to see how much you know all right so I think that that's going to be the end of the series for now like I said in the last video I am open to suggestions if you think you know if you have something that you think will fit in this series I hope you guys enjoyed it and I'd really appreciate it if you could give it a like and maybe share with some other people and that's it thanks so much for watching

Original Description

In this final video of the series, we will talk about JS engines and look at how JavaScript code is turned into machine code that runs on the CPU. 💻 My Courses & More https://traversymedia.com 💖 Show Support Patreon: https://www.patreon.com/traversymedia PayPal: https://paypal.me/traversymedia 👇 Follow Me On Social Media: Twitter: https://twitter.com/traversymedia Instagram: https://www.instagram.com/traversymedia Linkedin: https://www.linkedin.com/in/bradtraversy Timestamps: 0:00 - Intro 0:42 - What is a JS Engine? 1:31 - Specific Browser Engines 3:30 - Compiled vs Interpreted Languages 6:11 - JS Engine Process Overview 7:25 - Abstract Syntax Tree (AST) 10:01 - Interpreter & Bytecode 10:58 - JIT Compilation
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Traversy Media · Traversy Media · 0 of 60

← Previous Next →
1 Changing Your DNS/Nameservers
Changing Your DNS/Nameservers
Traversy Media
2 Create a MySQL database in cPanel
Create a MySQL database in cPanel
Traversy Media
3 Install & Uninstall Joomla Extensions
Install & Uninstall Joomla Extensions
Traversy Media
4 Adding and linking an article in Joomla
Adding and linking an article in Joomla
Traversy Media
5 Create a Joomla Blog
Create a Joomla Blog
Traversy Media
6 Import & Export A MySQL Database
Import & Export A MySQL Database
Traversy Media
7 Use A Custom Font On Your Website Using CSS
Use A Custom Font On Your Website Using CSS
Traversy Media
8 Connect Joomla Site With Dreamweaver
Connect Joomla Site With Dreamweaver
Traversy Media
9 Remove Phoca Gallery 3.2.3 Footer Text
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
10 Drupal 7 Security Update 7.19 to 7.20
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
11 Add An Addon Domain In Cpanel
Add An Addon Domain In Cpanel
Traversy Media
12 Pull A Heroku Rails App and Database
Pull A Heroku Rails App and Database
Traversy Media
13 Create a Custom Joomla 2.5 Module - Part 1
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
14 Create a Custom Joomla 2.5 Module - Part 2
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
15 Create a Custom Joomla 2.5 Module - Part 3
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
16 Joomla SEO Tutorial - sh404sef Configuration
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
17 Font Dragr
Font Dragr
Traversy Media
18 Convert an HTML Template to Joomla 2.5/3.0 - Part One
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
19 Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
20 Rockettheme Rocketlauncher   Joomla Site in Under 10 Minutes
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
21 JQuery FAQ Slider Tutorial
JQuery FAQ Slider Tutorial
Traversy Media
22 301 Redirect With htaccess File
301 Redirect With htaccess File
Traversy Media
23 Convert HTML to Wordpress Theme - Part 1
Convert HTML to Wordpress Theme - Part 1
Traversy Media
24 Convert HTML to Wordpress Theme - Part 2
Convert HTML to Wordpress Theme - Part 2
Traversy Media
25 Easy JQuery Widgets
Easy JQuery Widgets
Traversy Media
26 Codeigniter App Part 1 - Creating the Database
Codeigniter App Part 1 - Creating the Database
Traversy Media
27 Codeigniter App Part 2 - Installation and Configuration
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
28 Codeigniter App Part 6 - Login/Register System
Codeigniter App Part 6 - Login/Register System
Traversy Media
29 Codeigniter App Part 7 - Models List CRUD
Codeigniter App Part 7 - Models List CRUD
Traversy Media
30 Codeigniter App Part 8 - Models Task CRUD
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
31 Node.js Part 1 - Install NodeJS on Windows
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
32 Node.js Part 3 - Building a Static Page Server
Node.js Part 3 - Building a Static Page Server
Traversy Media
33 Node.js Part 4 - NPM
Node.js Part 4 - NPM
Traversy Media
34 Node.js Part 2 - Install MongoDB in Windows
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
35 Create a Joomla Quickstart with Custom Sample Data
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
36 Install MongoDB in Ubuntu
Install MongoDB in Ubuntu
Traversy Media
37 HTML5 Web Storage
HTML5 Web Storage
Traversy Media
38 Create a Joomla Bootstrap Template From Scratch
Create a Joomla Bootstrap Template From Scratch
Traversy Media
39 Ubuntu Server 14.04 Setup Part 1 - Installation
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
40 Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
41 Create A Wordpress Widget - Part 1
Create A Wordpress Widget - Part 1
Traversy Media
42 Create A Wordpress Widget - Part 2
Create A Wordpress Widget - Part 2
Traversy Media
43 Create A Wordpress Widget - Part 3
Create A Wordpress Widget - Part 3
Traversy Media
44 Create A Wordpress Widget - Part 4
Create A Wordpress Widget - Part 4
Traversy Media
45 Get Started With Sass on Windows
Get Started With Sass on Windows
Traversy Media
46 Build An HTML5 Template With Bootstrap and SASS - Part 1
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
47 Build An HTML5 Template With Bootstrap and SASS - Part 6
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
48 Build An HTML5 Template With Bootstrap and SASS - Part 4
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
49 Build An HTML5 Template With Bootstrap and SASS - Part 5
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
50 Build An HTML5 Template With Bootstrap and SASS - Part 3
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
51 Build An HTML5 Template With Bootstrap and SASS - Part 2
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
52 Build An HTML5 Template With Bootstrap and SASS - Part 7
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
53 Build An HTML5 Template With Bootstrap and SASS - Part 10
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
54 Build An HTML5 Template With Bootstrap and SASS - Part 8
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
55 Build An HTML5 Template With Bootstrap and SASS - Part 11
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
56 Build An HTML5 Template With Bootstrap and SASS - Part 9
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
57 Build An Audio Player Using HTML5 & jQuery - Part 1
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
58 Build An Audio Player Using HTML5 & jQuery - Part 2
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
59 Youtube Data API v3 & jQuery To List Channel Videos
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
60 Using Bootstrap With Ruby on Rails
Using Bootstrap With Ruby on Rails
Traversy Media

This video teaches the basics of JavaScript engines and how they interpret and execute JavaScript code, including the role of just-in-time compilation and abstract syntax trees. It provides a comprehensive overview of the JavaScript engine and its components.

Key Takeaways
  1. Write JavaScript source code
  2. Pass source code to parser for syntax checking
  3. Generate abstract syntax tree (AST) if code is valid
  4. Use AST Explorer website to view AST of code
  5. Add function to code and view updated AST
  6. Understand Interpreter's role in transforming AST to byte code
💡 Just-in-time compilation is a crucial optimization technique used by JavaScript engines to improve code execution performance

Related AI Lessons

What OOP Actually Buys You (And Why “Real World Modeling” Is a Lie)
Learn the actual benefits of Object-Oriented Programming (OOP) and why 'real world modeling' is a misconception
Medium · Programming
Data Partitioning in System Design: Why Every Scalable Application Depends on It
Learn how data partitioning enables scalable applications to handle growth without failing
Medium · Programming
Why Realtime Collaboration Is Harder Than It Looks?
Realtime collaboration is a complex distributed systems problem that requires careful engineering, not just a simple UI feature
Medium · JavaScript
Podcast: Architectural Patterns: Moving Beyond Cloud-Native to Local-First - Insights from Adam Wiggins
Learn how to design local-first architectures that combine cloud-based collaboration with local software performance and data ownership
InfoQ AI/ML

Chapters (8)

Intro
0:42 What is a JS Engine?
1:31 Specific Browser Engines
3:30 Compiled vs Interpreted Languages
6:11 JS Engine Process Overview
7:25 Abstract Syntax Tree (AST)
10:01 Interpreter & Bytecode
10:58 JIT Compilation
Up next
Retracing It All With My Son
Ginny Clarke
Watch →