Top Banner
ALL YOU NEED TO KNOW ABOUT JAVASCRIPT LOADING AND EXECUTION IN THE BROWSER <SCRIPT language="JavaScript"> //<![CDATA[ alert(' '); //]]> </SCRIPT>
100

All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

Jan 15, 2015

Download

Technology

Caelum

Techniques for fast and modular JS loading. Talk presented at JSConf Brazil 2013 in Fortaleza by Sérgio Lopes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

ALL YOU NEED TO KNOW ABOUTJAVASCRIPTLOADING AND EXECUTIONIN THE BROWSER

<SCRIPT language="JavaScript">//<![CDATA[alert('

');//]]></SCRIPT>

Page 2: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

@sergio_caelumsergiolopes.org

Page 3: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="/js/main.js"></script>

Page 4: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

THE END

Page 5: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

THE END?

Page 6: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

HOW BROWSERS LOAD AND EXECUTE JS

Page 7: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

disclaimer

Page 8: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 9: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 10: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

HOW BROWSERS LOAD AND EXECUTE JS

Page 11: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<html><head> <script src="main.js"></script></head><body> ...</body></html>

Page 12: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

JS BLOCKS RENDERING

Page 13: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 14: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 15: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 16: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

JS BLOCKS RENDERING

Page 17: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

JS BLOCKS RENDERINGNETWORK LATENCY

PARSING TIMEEXECUTION TIME

AVAILABILITY (SPOF)

Page 18: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

PUT JS AT BOTTOM

Page 19: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<html><head> <script src="main.js"></script></head><body> ...

</body></html>

Page 20: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<html><head>

</head><body> ... <script src="main.js"></script></body></html>

Page 21: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

DEFER

Page 22: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<html><head> <script src="main.js" defer></script></head><body> ...</body></html>

Page 23: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<html><head> <script src="main.js" defer></script> <script src="other.js" defer></script></head><body> ...</body></html>

Page 24: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

PUT JS AT BOTTOMPLUS: NO NEED FOR $(document).ready(..) ANYMORE

Page 25: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

MULTIPLE SCRIPTS

Page 26: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="jquery.js"></script><script src="jquery.plugin.js"></script><script src="application.js"></script>...

Page 27: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 28: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

LATENCY

Page 29: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

SCRIPT CONCATENATION

Page 30: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

SCRIPT CONCATENATION1 SCRIPT? 2 SCRIPTs?

Page 31: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

SEQUENTIAL EXECUTION

Page 32: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 33: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

ASYNC LOADING

Page 34: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var js = document.createElement('script');js.src = 'script.js';document.head.appendChild(js);

Page 35: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="main.js" async></script><script src="other.js" async></script>

Page 36: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<html><head> <script src="main.js" async></script> <script src="other.js" async></script></head><body>

</body></html>

Page 37: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

EXECUTION ORDER?

Page 38: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

SEPARATE DOWNLOAD FROM EXECUTION

Page 39: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="main.js" type="bla"></script>

Page 40: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="main.js"></script>

Page 41: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="main.js"></script>

/* alert('Everything commented out'); ... */

Page 42: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="main.js"></script>

/* alert('Everything commented out'); ... */

eval(js_code_inside_comments);

Page 43: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

new Image().src = 'script.js';

Page 44: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

new Image().src = 'script.js';

<script src="script.js"></script>

Page 45: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function(js){ if (ajax.readyState == 4) // execute js in order};

ajax.open('script.js');ajax.send();

Ajax

Page 46: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var js = document.createElement('script');js.src = 'script.js';

IE

Page 47: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var js = document.createElement('script');

js.src = 'script.js';

IE

js.onreadystatechange = function(){ if (js.readyState == 'loaded') document.head.appendChild(js);};

Page 48: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var js = document.createElement('script');js.preload = true;js.onpreload = function(){ // ...};

js.src = 'script.js';

preload=true

Page 49: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var js = document.createElement('script');js.async = false;js.src = 'script.js';document.head.appendChild(js);

async=false

Page 50: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

SCRIPT LOADERS

Page 51: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

$LAB.script('jquery.js').wait() .script('plugin1.js') .script('plugin2.js') .script('plugin3.js').wait() .script('application.js');

LABjs

Page 52: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

DISCOVERABILITY

Page 53: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="labjs.js"></script>

<script>$LAB.script('jquery.js').wait() .script('plugin1.js') .script('plugin2.js') .script('plugin3.js').wait() .script('application.js');</script>

Page 54: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script>/* inline ~2KB do LABjs */</script>

<script>$LAB.script('jquery.js').wait() .script('plugin1.js') .script('plugin2.js') .script('plugin3.js').wait() .script('application.js');</script>

Page 55: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

LOOK AHEAD PRE-PARSER

Page 56: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<link rel="prefetch" href="script.js">

Page 57: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<link rel="subresource" href="script.js">

Page 58: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<link rel="subresource prefetch" href="script.js">

Page 59: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

ASYNC EXECUTION

Page 60: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="script1.js" async></script><script src="script2.js" async></script><script src="script3.js" async></script>

Page 61: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

DEPENDENCIES?

Page 62: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

order != dependency

Page 63: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="script1.js"></script><script src="script2.js"></script><script src="script3.js"></script><script src="script4.js"></script><script src="script5.js"></script>

Page 64: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

AMDASYNCHRONOUS MODULE DEFINITION

* OR SOMETHING SIMILAR

Page 65: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

$('#panel').fadein();

Page 66: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

define('app', ['jquery'], function($) {

}); $('#panel').fadein();

Page 67: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var jQuery = // ...

Page 68: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

var jQuery = // ... function() {

return jQuery;}

Page 69: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

define('jquery',[],

);

var jQuery = // ... function() {

return jQuery;}

Page 70: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

BETTER CODE

Page 71: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

ASYNC DEPENDENCY EXECUTION

Page 72: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

Require.js

Page 73: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="require.js" data-main= "myapp"></script>

Page 74: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="require.js" data-main= "myapp"></script>

myapp.js

Page 75: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="require.js" data-main= "myapp"></script>

myapp.js

mymodel.js mycontroller.js

Page 76: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="require.js" data-main= "myapp"></script>

myapp.js

mymodel.js mycontroller.js

util.js plugin1.js plugin2.js

Page 77: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="require.js" data-main= "myapp"></script>

myapp.js

mymodel.js mycontroller.js

jquery.js

util.js plugin1.js plugin2.js

Page 78: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

BUILD

Page 79: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

r.js + almond

Page 80: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

r.js + almondr.js -o baseUrl=. name=almond include=main out=all.js wrap=true

Page 81: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script src="all.js"></script>

myapp.js

mymodel.js mycontroller.js

jquery.js

util.js plugin1.js plugin2.js

all.js

almond.js

Page 82: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

myapp.js

mymodel.js mycontroller.js

jquery.js

util.js plugin1.js plugin2.js

almond.jsbase.js

mypage.js

<script src="base.js"></script><script src="mypage.js"></script>

Page 83: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script>/* 250b AMD define */</script>

<script src="base.js" async></script><script src="modules.js" async></script><script src="more.js" async></script>

Page 84: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

FUTURE

Page 85: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

TEST: 200 JS modules, ~13KB each

#1200 .js files + 1 HTML file<script async>HTTP

Page 86: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 87: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

TEST: 200 JS modules, ~13KB each

#21 .js file + 1 HTML file<script async>HTTP

Page 88: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 89: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

TEST: 200 JS modules, ~13KB each

#3200 .js files + 1 HTML file<script async>SPDY

Page 90: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013
Page 91: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

200 files

1 file

0 12500 25000 37500 50000

HTTP:

Page 92: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

200 files

1 file

200 files

1 file

0 12500 25000 37500 50000

HTTP:

SPDY:

Page 93: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

SPDY & HTTP 2.0

Page 94: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

FUTURE #2

Page 95: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

module utils { var local = 0; export var exposed = "Public API";}

Modules

Page 96: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

Modules

module main { import exposed from utils; console.log(exposed);}

Page 97: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

ES-HARMONY

Page 98: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

<script>deferasync

SCRIPT LOADERSprefetch / subresource

AMDrequire.js / r.js / almond

SPDY & HTTP 2ES-HARMONY MODULES *

Page 99: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

ALL YOU NEED TO KNOW ABOUTJAVASCRIPTLOADING AND EXECUTIONIN THE BROWSER

<SCRIPT language="JavaScript">//<![CDATA[alert('

');//]]></SCRIPT>

Page 100: All you need to know about JavaScript loading and execution in the browser - JSConf BR 2013

THANK YOUsergiolopes.org

@sergio_caelum

<SCRIPT language="JavaScript">//<![CDATA[alert('

');//]]></SCRIPT>