Skip to main content

Command Palette

Search for a command to run...

How a Browser Works: A Beginner-Friendly Guide to Browser Internals

Published
3 min read
How a Browser Works: A Beginner-Friendly Guide to Browser Internals
L

Learning in public

UI : Anything we see but can’t manipulate that like adderss-bar , setting. UI works with UI Backend(designed to develop basic widgets).

Browser Engine: Works as a midator between UI and Rendering engine. It do work like refresh etc.

Rendering Engine : Used to parse HTML, CSS and JS . And display the page .

Networking : Load requested page from internet

JavaScript Interpreter : Interprets JavaScript

Data Persistance: Local Storage, Index DB, File System etc.

Basic Rendering Engine Flow

Parsing

Parsing: Translating a document into a structure that code can use.

w=1250 (1020×290)

Grammer :

  • Vocabulary : 1, +, 2, *, 3

  • Syntax Rules : how vocabulary interacts

Why html does not use conventional parser ?

  • HTML is not context free grammer (HTML immediatly recover from the error on the way of parser)

    Like in this example : Both will work

      <body>
          <div>
              <p>
                  Hello 
              </p>
          </div>
      </body>
    
      <body>
          <div>
              <p>
                  Hello 
      </body>
    
  • The Syntax Rules defined in HTML Document Type Defination(DTD) by W3C

Render tree

  • Render tree generated while DOM tree is constructed

  • Visualize elements in the order which they are going to be displayed

  • Elements in the render tree are called renderer or render dobjects

  • Render object is a rectangle

Layout

  • Layout is also called re-flow in mozila browser

  • Calculates position and size

  • Most of the time possible to compute geometry in one pass

  • Recursive process begins at the root object(<html>)

Dirt bit System - a system that makes sure that browsers don’t need to do the full layout on every interaction(only re-layout is done on the changed part of the DOM).

Global and incremental layout Global :

  • affects all renders(font size)

  • screen resize

Gecko Reflow Visualization:

Paint

Render tree is being traversed and the paint() method is used to display content on the page.

Global(the whole page is repainted) and Incremental painting(only changed part is repainted):

  • reduce the effort of painting the whole thing

  • dirty bit system(incremental painting)

Painting Order:

  • Background color

  • Background image

  • Border

  • Children

  • Outline

How DOM is created from HTML

  1. Load The File

  2. Convert to Raw bytes(this is how data is saved in disk)

  3. Convert into Character information(based on UTF)

  4. Then Converted into Token(Tokenization) —> h1, p, html, body

  5. Converting into literal object:

     {
     tag: 'h1',
     title: 'heading',
     data/value: 'youtube'
     }
    
     {p object}
     {body object}
    
  6. Convertion of object into structure is called Modal

  7. Relation is enstablished

  8. Coverted into NodeList

  9. Now We have DOM

How CSSOM is created from CSS

  1. Raw Byte

  2. Converted Into Character

  3. Tokenization

  4. Create Object out of it

  5. Convertion of object into structure is called Modal

  6. Relation is enstablished

  7. Now we have CSSOM

Extra Notes:

  • DOM And CSSOM is runs parallelly and independent of each other

  • JS get more priority than DOM while rendering

  • What if the CSSOM not yet ready and we hit the JS tag ?

--> JS execution will be halted until CSSOM is ready

  • CSS is Render Blocking

  • JS is parser blocking

References: