Browser Architecture
Browser Architecture
hotstar.com/in/movies/the-lion-king/1660000046
WELCOME
Browser Architecture under the hood:
How webpages work in modern-day
+
The Lion King - Hotstar Premium
+
hotstar.com/in/movies/the-lion-king/1660000046
Who am I?
Who am I?
Ankush Dharkar
@ankushdharkar
Technical Cofounder
https://yudek.com/
Ex- Web Developer
https://hotstar.com/
Coach / Mentor, Real Dev Squad
https://realdevsquad.com
Why do you need a
browser?
What is a browser?
Internet
✨ Magic⚡✨
How does a website
load?
Specialized
Software
Documents
Images
JS
CSS
Document
HTML
HTML
Hyper
Text
Markup
Language
Hyper-links
https://www.hotstar.com/
https://www.meetup.com/
https://www.google.com/
Markup
Tags
Tags
<button>
Login
</button>
Attribute
<button class=“btn-login”>
Login
</button>
3. Build
What should show up?
Critical Rendering
Path
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<button class="btn-login"> Login </button>
<script src="script.js"></script>
</body>
</html>
DOM
window
document
html
head body
h1 img p p
CSSOM
body
font-size: 16px
font-size: 16px
h1 img p p
font-size: 32px font-size: 20px font-size: 20px
1. Browser examines your HTML and
builds the DOM
Data Persistence
Browser Engine
Rendering Engine
Networking JS UI Backend
Browser engine
• Creates layout
Networking
• Communication + security
• Caching
JS interpreter
• JS Code
OS
Hardware
Application
Process
Application that’s currently executing its code
Browser
Memory
The Lion King - Hotstar
+
hotstar.com/in/movies/the-lion-king/
Process
Browser Architecture
One process
The Lion King - Hotstar
+
hotstar.com/in/movies/the-lion-king/1660000046
Networking
Storage
UI Painting
Multi-process
UI UI
Network Process Browser Process UI Process
UI UI
Device Process
Plugin Process
Rendering Process
Architectures are just
implementations of Standards
’s Architecture
Storage
UI
UI
Networking
Utility Process
Browser Process
GPU Process
Main Compositor
Renderer Process
Plugin Process
The Lion King - Hotstar Premium All about JS prototype +
hotstar.com/in/movies/the-lion-king/1660000046
Browser Process
Render Process
Plugin Process
GPU Process
Servicification in Chrome
Processes have their own private memory
space
• Security
• Sandboxing
Operating systems provide a way to restrict
processes’ privileges
1. Handling Input
2. Start Navigation
3. Read Response
4. Renderer process
5. Commit navigation
The Lion King - Hotstar Premium +
hotstar.com/in/movies/the-lion-king/1660000046
UI
Browser Process
UI thread initiates a network call to get site
content
Content-Type header should say what type of data it is, but since it
may be missing or wrong, additionally MIME Type Sniffing may be
done
header
Content-Type: text/html
Content-Length: 44235
payload
f5 a3 b6 b0 02 2a 4c 33 f1
b6 b7 c1 10 2d ba c1 4a 64
...
Checks:
• SafeBrowsing
When the UI thread is sending a URL request to the network thread, it already
knows which site they are navigating to
The UI thread tries to proactively find or start a renderer process in parallel to the
network request
This standby process might not get used if the navigation redirects cross-site, in
which case a different process might be needed
• Data stream via IPC for HTML
Round trip between the browser process and renderer process could result
in delays, if service worker eventually decides to request data from the
network
Convert HTML, CSS, and JavaScript into a web page that the user can
interact with
In a renderer process, the main thread handles most of the code you send
to the user (except Web/Service worker threads)
• defer
<link rel="preload">
Style calculation
Layout
Viewport
Paint
Rectangle invalidation
• Transforms
• Opacity
Style
Layout
Paint
Composite
Frame budgets for 60 fps+
Input events
From the browser's point of view, input means any gesture
from the user
Browser
Event, (x,y)
Coordinates
Renderer
Find event target,
run event handlers
If no input event listeners are attached to the page,
Compositor thread can create a new composite frame
completely independent of the main thread.
https://developers.google.com/web/updates/2018/09/inside-browser-part4
Chrome coalesces continuous events and delays dispatching
until right before the next requestAnimationFrame
(getCoalescedEvents)
https://developers.google.com/web/updates/2018/09/inside-browser-part4
Parallelism problem
Coarse-grained
Parallelism
Idle 0%
- Lin Clark
Firefox started investing in research
Rust
A browser engine — Servo
that made full use of this fine-grained parallelism
Electrolysis
Most of this work has landed, but they still plan to take this
further with something called pre-emptive scheduling.
Quantum Compositor
Compositor to its own process
• Selector matching
• Cascade
Struct Style Sharing
Color
Color
color: blue;
Margin
Font
Margin
margin-left: 20px;
margin-right: 20px;
RULE tree
root
div span
(stylesheet)
div .red p
div.warning div.error
On restyle, the engine does a quick check to see
whether the change to the parent could
potentially change the rules that match children
If not, then for any descendants, the engine can
just follow the pointer on the descendant node to
get to that rule
COMPUTED #23
section section
p p p p p p p p p
COMPUTED #52
COMPUTED #119
e.g :first-child
: rst- :last-
child child
0 1 0 0
0 1 1 0
fi
JS Engine Optimizations
JS Engine Pipeline
Source
Parser AST
Code
Deoptimize Optimize
Profiling Data
Optimized Optimizing
code Compiler
Optimized
Byte Code
code
Generated Generated
quickly slower
Optimizing Optimized
Compiler code
V8
Optimized
Turbofan
code
Spidermonkey
Optimize
Somewhat
Baseline Optimized
code
Optimized
IonMonkey
code
Chakra
Optimize
Profiling Data
Deoptimize
Somewhat
SimpleJIT Optimized
Optimize code
Profiling Data
Optimize
Optimized
FullJIT
code
JavascriptCore (JSC)
Safari, React Native
LLInt bytecode
Optimize
somewhat
Baseline optimized
code
mostly
DFG optimized Deoptimize
code
Optimized
FTL code
JS is dynamic
[[Value]]: ‘Alex’
[[Writable]]: true
Object [[Enumerable]]: true
[[Con gurable]]: true
‘name’
Property Attributes
‘age’
[[Value]]: 29
[[Writable]]: true
[[Enumerable]]: true
[[Con gurable]]: true
fi
fi
user1 = { name: 'Alex', age: 29 }
‘age’ ‘age’
[[Value]]: 29 [[Value]]: 37
[[Writable]]: true [[Writable]]: true
[[Enumerable]]: true [[Enumerable]]: true
[[Con gurable]]: true [[Con gurable]]: true
fi
fi
fi
fi
validUserNames = [ ‘Alex’, ‘Rahul’ ];
[[Value]]: 2
Array
[[Writable]]: true
[[Enumerable]]: false
‘length’ [[Con gurable]]: false
‘0’
[[Value]]: ‘Alex’
[[Writable]]: true
‘1’ [[Enumerable]]: true
[[Con gurable]]: true
[[Value]]: ‘Rahul’
[[Writable]]: true
[[Enumerable]]: true
[[Con gurable]]: true
fi
fi
fi
Object Object
‘name’ ‘name’
‘age’ ‘age’
Shape
‘name’
‘age’
Shape
O set: 1
[[Writable]]: true
‘name’
[[Enumerable]]: true
[[Con gurable]]: true
‘age’
Property Information
ff
fi
JSObject
‘Alex’
29 O set: 1
[[Writable]]: true
Shape [[Enumerable]]: true
[[Con gurable]]: true
‘name’
Property
Information
JSObject ‘age’
Rahul
user1 = { name: 'Alex', age: 29 }
user2 = { name: 'Rahul', age: 37 }
39
ff
fi
Object Shapes
“hidden classes”
SpiderMonkey: Shape
V8: Maps
JSC: Structure
Shape(empty)
user1 = {}
‘name’
user1.name ='Alex'
user1.age = 29
Shape(name)
O set: 0
‘name’
[[Writable]]: true
JSObject
[[Enumerable]]: true
‘age’ [[Con gurable]]: true
‘Alex’
Shape(name, age)
29
‘name’ O set: 1
[[Writable]]: true
function getAge(user) {
return user.age;
}
Byte Code
Shape(name, age) O set: 0
[[Writable]]: true
‘name’ [[Enumerable]]: true
[[Con gurable]]: true
‘age’
O set: 1
[[Writable]]: true
[[Enumerable]]: true
function getAge(user) { [[Con gurable]]: true
return user.age;
}
function getAge(user) {
return user.age;
}
arrUsers.forEach(usr => {
var age = getAge(usr)
// doSomething(age)
})
Same shape
getFullName({ firstName: ‘Alex’, lastName: ‘Super’ })
getFullName(Alex’, ‘Super’)
Where should I
start?
Inspect Elements
Console
Debugger
Network Tab
Application
Performance
Auditing
React
https://reactjs.org/
MDN Docs
https://developer.mozilla.org/
Don’t skip learning
Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript
• Parse
• Style
• Layout
• Paint
• Composite
• Render
https://hacks.mozilla.org/2017/05/quantum-up-close-what-is-a-browser-engine/
Ref
https://hacks.mozilla.org/2017/05/quantum-up-close-what-is-a-browser-engine/
https://hacks.mozilla.org/2017/11/entering-the-quantum-era-how- refox-got-fast-again-and-where-
its-going-to-get-faster/
https://hacks.mozilla.org/2017/08/inside-a-super-fast-css-engine-quantum-css-aka-stylo/
https://hacks.mozilla.org/2017/10/the-whole-web-at-maximum-fps-how-webrender-gets-rid-of-jank/
https://www.slideshare.net/quangntta/web-browser-architecture-49196378
https://v8.dev/features/modules
https://developers.google.com/web/fundamentals/performance/rendering/stick-to-compositor-only-
properties-and-manage-layer-count
https://developers.google.com/web/updates/2018/09/inside-browser-part1
https://mathiasbynens.be/notes/shapes-ics
https://hacks.mozilla.org/2017/08/inside-a-super-fast-css-engine-quantum-css-aka-stylo/
https://www.youtube.com/watch?v=cCOL7MC4Pl0
fi
QUESTIONS?
THANK YOU !
@ankushdharkar