Naser Hassani
Front-End Developer
by understanding Browsers
May 2016
TehranJS
Seyed Naser Hassani
Freelance Frontend Developer
Chrome, Internet Explorer, Firefox, Safari and Opera
Open Source
engine
Chrome, Opera (blink)
Firefox (gecko)
Safari (webkit)
“present the web resource you choose, by requesting it from the server and displaying it in the browser window."
The responsibility of the rendering engine is well... Rendering, that is display of the requested contents on the browser screen.
Parsing HTML to construct the DOM tree
Tokenize the response
Parse HTML
Build DOM tree
<html>
<body>
<p>
Hello World
</p>
<div><img src="example.png"/></div>
</body>
</html>
Parsing HTML to construct the DOM tree
Render tree construction
Tokenize the response
Parse HTML
Build DOM tree
+
NOT PART OF RENDER TREE
User Styles
Inline Styles
Browser Styles
Parsing HTML to construct the DOM tree
Render tree construction
Layout of the render tree
Parsing HTML to construct the DOM tree
Render tree construction
Layout of the render tree
Painting the render tree
1000ms / 60 fps = 16ms ~ 10 - 12ms
is compilation done during execution of a program – at run time – rather than prior to execution.
for (var i = 0; i < arr.length; i++) {
// do stuff
}
for (var i = 0; len = arr.length; i < len; i++){
// do stuff
}
VS
if you can trivially optimize
it, the browser (probably)
can too!
function animation() {
// Do something super rad here.
requestAnimationFrame(animation);
}
requestAnimationFrame(animation);
var dataSortWorker =
new Worker('sort-worker.js');
dataSortWorker.postMessage(dataToSort);
// The main thread is now free to
// continue working on other things...
dataSortWorker.addEventListener( 'message',
function(evt) {
var sortData = evt.data;
// Update data on screen in a rAF...
});
var taskList = breakTaskDown(monsterTaskList);
requestAnimationFrame(processTaskList);
function processTaskList(taskStartTime){
var taskFinishTime;
do {
var nextTask = taskList.pop();
processTask(nextTask);
// Go again if there's enough time...
taskFinishTime = window.preformance.now();
} while (taskFinishTime - taskStartTime < 3);
if (taskList.lenght > 0)
requestAnimationFrame(processTaskList);
}
"But worker don't have DOM access."
2,219KB
The average weight of a web page
images
js
video
font
css
80.5 %
1,421 KB
368 KB
177 KB
116 KB
74 KB
52 KB
Current JavaScript delivery techniques focus on delivering a single package
This means on average websites are downloading 368kb of JavaScript before any can be executed
If the JavaScript hasn’t
loaded, the user can’t interact
with things on the page that
depend upon it
As we are building a responsive site, today’s example will aim to start rendering in 5 seconds on a slow 3G connection
WebPageTest defines this as 96 kilobytes per second
To start with we need to define ‘slow 3G’
96KB x 5secs = 480KB
So as we want to load our site in 5 seconds...
We can then split 480KB across our assets
HTML | CSS | JS | Images | Fonts |
---|---|---|---|---|
30KB | 40KB | 50KB | 300KB | 60KB |
<link type="text/stylesheet" href="/main.css"/>
<script>
window.getComputedStyle(document.body).margin;
</script>
CSSOM + inline script
can’t execute until CSS ready
<link type="text/stylesheet" href="/main.css"/>
<script>
var script = document.createElement('script');
script.src = '/app.js';
document.getElementsByTagName('head')[0]
.appendChild(script);
</script>
CSSOM + dynamic script insertion
can’t execute until CSS ready
dynamic script insertion
blocking
<link type="text/stylesheet" href="/main.css"/>
<script async src="/app.js"></script>
3rd option: async attribute
ideal: async attribute
console.thanks('THE END');
QUESTION?
By Naser Hassani
How Browsers Work: Behind the scenes of modern web browsers