Bare Metal JavaScript: The JavaScript Virtual Machine

Miško Hevery

Miško Hevery

Qwik Creator (Previously Angular)
3 hours, 21 minutes CC
Bare Metal JavaScript: The JavaScript Virtual Machine

Course Description

Ever wondered how high-level JavaScript turns into low-level CPU instructions? Learn the fundamentals of CPU mechanics to see how code runs through memory operations, inline caching, and deoptimization. You'll build a mental model of JavaScript's performance characteristics by learning how virtual machines (like Google's V8 engine) work under the hood!

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: October 17, 2023

Learning Paths

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Introduction

Section Duration: 5 minutes
  • Introduction
    Miško Hevery provides an overview of the course topic, how JavaScript virtual machines connect physical computer hardware with the virtual world of JavaScript. Course topics include performance implications, double vs. triple equals, function calls, and arrays with holes.

How the CPU Runs Code

Section Duration: 1 hour, 3 minutes
  • Physical & Virtual Machines
    Miško discusses the virtual machine's role in the communication between code and the CPU. Parts of the CPU, including registers, arithmetic logic unit, and program counter and how they interact with memory, is also covered in this segment
  • CPU Basics & Speed Limits
    Miško walks through the basics of a CPU, including its registers and memory operations. The limitations of CPU speed and the techniques used to make CPUs faster, such as increasing clock speed, parallelism, and adding more cores.
  • CPU Memory Management
    Miško explores memory management and introduces the concept of a program section and a data section in CPU memory. How CPU registers are used to load, store, and manipulate data within these memory sections is also discussed in this segment.
  • CPU Subroutines & Calling Conventions
    Miško explains the concept of subroutines, their primitive nature compared to functions, and the mechanics of calling a subroutine. How data is passed, how registers are managed, the risks of stack overflow, and the role of the stack pointer in preserving program state are also discussed in this segment.
  • CPU Q&A
    Miško answers student questions about the concept of computer RAM being similar to a massive array, how different registers within a CPU play specialized roles in performing operations, and how hardware architecture varies between CPU types. JavaScript engines, bridging the gap between high-level programming languages and low-level hardware operations is also covered in this segment.
  • Fibonacci
    Miško discusses the transition from low-level machine language to assembly language in computer programming, how assembly language provides a more human-readable and manageable way to work with machine-level instructions. The concept of subroutine calls and recursion using an example of implementing the Fibonacci sequence in assembly language is also covered in this segment.

Virtual Machine Inlining & Deopt

Section Duration: 43 minutes
  • Optimizing Code
    Miško discusses the trade-off between speed and memory in programming, how modern virtual machines analyze code for constants, inline functions for optimization, and deopt. A student's question regarding the performance of function declaration compared to function expression is also covered in this segment.
  • Deopt Explorer & Fixing a Deopt
    Miško introduces a tool called deopt explorer, which provides insights into the performance of JavaScript code running in V8, the JavaScript engine used in Node.js. How to generate a log of V8's compilation and optimization events and how to analyze this log using deopt explorer is also covered in this segment.
  • Object Shapes & Hidden Classes
    Miško discusses the performance implications of property reads in JavaScript objects and the impact of polymorphic behavior and inline caching. How JavaScript objects are represented internally in V8 and how hidden classes are used to optimize property access are also discussed in this segment.

Micro Benchmarking

Section Duration: 1 hour, 19 minutes
  • Object Shapes & Inline Caching
    Miško demonstrates the performance implications of using inline caching, discusses how inline caches work, and how they can optimize property access. Consider the shape of objects in your code and how inline caches can impact real-world performance, especially in complex frameworks and applications.
  • Inspecting Inline Caches with Deopt Explorer
    Miško demonstrates using deopt explorer to analyze JavaScript code, highlighting various aspects such as inline caches, deoptimizations, and object shapes The significance of monomorphic and polymorphic reads in optimizing code execution is also discussed in this segment.
  • Double & Triple Equals
    Miško discusses how JavaScript's equality operators, triple equals (strict equality) is generally faster than double equals (loose equality) due to the absence of type coercion. A demonstration of how adding properties to objects or prototypes can unexpectedly impact the performance of double equals comparisons is also provided in this segment.
  • Object & Array Copying
    Miško discusses using JSX, which converts into object literals, leading to potential performance issues when converting these object literals into DOM attributes. Benchmark comparisons between different approaches, such as using objects, arrays, and copying objects, to highlight the performance differences and considerations when working with JSX in real applications are also provided in this segment.
  • Negative Numbers
    Miško explores a JavaScript behavior where using negative zero (-0) can lead to slower performance compared to performing zero minus X (0 - X). This phenomenon arises from how JavaScript handles negative zero as a floating-point number, which can disrupt compiler optimizations when accessing properties or arrays with integers, resulting in performance differences between browsers.
  • Holey Arrays
    Miško discusses the concept of "holy arrays" in JavaScript, which are arrays that contain holes or undefined values. Reading values from holy arrays can lead to performance issues due to the need to traverse the prototype chain to resolve undefined values, making optimization challenging for JavaScript engines.
  • Amdahl’s Law
    Miško discusses Amdahl's Law, which highlights the importance of optimizing code that has a significant impact on the overall application performance rather than just focusing on small, infrequently executed portions. How modern CPUs and virtual machines work together to create the illusion of running JavaScript is also discussed in this segment.

Wrapping Up

Section Duration: 9 minutes
  • Wrapping Up
    Miško wraps up the course by discussing tools for static analysis and performance evaluation. The complexities of CPU caching and modern compiler optimizations is also covered in this segment.
I'm not sure why I enjoy learning the intricacies, innovations, and changes going on in the JavaScript Virtual Machine, but Misko Hevery and his insights in the course ‘Bare Metal JavaScript’ on Frontend Masters make it seem fun.
Always_Staples

Always_Staples

Always_Staples

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now