Slide 1

Slide 1 text

How late is later?

Slide 2

Slide 2 text

A lazy loading solution on the edge between very clever and incredibly mad." " Massimiliano Marcon" @mmarcon" " frontendconf.ch • September 7th 2012

Slide 3

Slide 3 text

What I do at Nokia Maps

Slide 4

Slide 4 text

I " LAZY " LOADING

Slide 5

Slide 5 text

WTF !!1!!! !! !!2!!! !! !!3!!!! !!4!!! !! !!5!!!!! !!6!!!!! !!7!!!! !!8!!! !! !!9!!! !! !10!!! !! !11!!! !! !12!!!!! !13!! !!

Slide 6

Slide 6 text

Not good either !1!! !!

Slide 7

Slide 7 text

The problem The amount of code the browser loads affects performance and influences the perceived loading time.

Slide 8

Slide 8 text

LESS CODE TO LOAD

Slide 9

Slide 9 text

Is all that code really necessary? NO!

Slide 10

Slide 10 text

THE STORY OF A HAPPY USER

Slide 11

Slide 11 text

I, as a user… Open my browser and start typing a URL in my address bar.

Slide 12

Slide 12 text

Bootstrap ASAP 1!

Slide 13

Slide 13 text

Dude, that was fast!

Slide 14

Slide 14 text

2! Enrich the Experience

Slide 15

Slide 15 text

3! Load remaining code on demand

Slide 16

Slide 16 text

Load shims on demand

Slide 17

Slide 17 text

1!Bootstrap ! 2!Enrich! 3!On Demand!

Slide 18

Slide 18 text

WHERE EVERYTHING BEGAN

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

…possibly load UI components on demand •  What does on demand mean?

Slide 21

Slide 21 text

“In distributed computing, code on demand is a general term for any technology that sends executable software code from a server computer to a client computer upon request from the client's software” ― Wikipedia

Slide 22

Slide 22 text

HOW CAN WE DO IT?

Slide 23

Slide 23 text

Require.js Modules ON DEMAND Dependencies

Slide 24

Slide 24 text

callback-require !1!!require(['require',! !! !2!!!!!!!!!!!'modules/3rd:party/jquery'],!function(require,!$)!{! !! !3!!!!!!$('button').on('click',!function(e){! !! !4!!!!!!!!!!require(['modules/dialog'],!function(dialog){! !! !5!!!!!!!!!!!!!!dialog.create($('

Hello!world

'),!'My!Dialog');! !! !6!!!!!!!!!!});! !! !7!!!!!!});! !! !8!!});!!

Slide 25

Slide 25 text

callback-require for shims !!1! !require(['require',! !! !!2! !!!!!!!!!!'modules/3rd:party/jquery'],!function(require,!$)!{! !! !!3! !! !! !!4! !!!!!var!SUPER_DUPER_APP!=!function(JSON){! !! !!5! !!!!!!!!!//Do!some!fancy!stuff!here!with!my!application.! !! !!6! !!!!!!!!!//At!this!poit!all!JSON.parse/JSON.stringify!will!be!available!for! !! !!7! !!!!!!!!!//ALL!browsers.!Isn't!that!cool?!! !! !!8! !!!!!};! !! !!9! !! !! !10! !!!!!if(typeof!JSON.parse!===!'function')!{!!! !11! !!!!!!!!!//Oh,!we've!got!native!JSON!support! !! !12! !!!!!!!!!SUPER_DUPER_APP(JSON!/*Native!one*/);! !! !13! !!!!!}! !! !14! !!!!!else!{! !! !15! !!!!!!!!!//Uhmm...!I!smell!IE!

Slide 26

Slide 26 text

CLEVERNESS or MADNESS?

Slide 27

Slide 27 text

Custom solution !1!!$(function(){! !! !2!!!!!!$('button').on('click',!function(e){! !! !3!!!!!!!!!!$.onDemand.use('dialog').done(function(dialog){! !! !4!!!!!!!!!!!!!!dialog.create($('

Hello!world

'),!'My!Dialog');! !! !5!!!!!!!!!!});! !! !6!!!!!!});! !! !7!!});!!

Slide 28

Slide 28 text

How does this work? !!1! !OnDemand.prototyte.firstLoad!=!function(fn,7args,7context){! !! !!2! !!!!!var!script,!deferred!=!$.Deferred(),!rv;!!! !!3! !! !! !!4! !!!!!if!(isFn(that._proxy[fn]))!{! !! !!5! !!!!!!!!!rv!=!that._proxy[fn].apply(context,!args);! !! !!6! !!!!!!!!!deferred.resolve(rv);! !! !!7! !!!!!}! !! !!8! !!!!!else!if(isObj(that._proxy[fn]))!{! !! !!9! !!!!!!!!!deferred.resolve(that._proxy[fn]);! !! !10! !!!!!}! !! !11! !!!!!else!{! !! !12! !!!!!!!!!script!=!this.fn2script(fn);! !! !13! !!!!!!!!!if!(!script)!{! !! !14! !!!!!!!!!!!!!return;! !! !15! !!!!!!!!!}! !! !16! !!!!!!!!!$.getScript(script).done(function(){!!! !17! !!!!!!!!!!!!!var!rv;! !! !18! !!!!!!!!!!!!!if!(isFn(that._proxy[fn]))!{! !! !19! !!!!!!!!!!!!!!!!!rv!=!that._proxy[fn].apply(context,!args);! !! !20! !!!!!!!!!!!!!}! !! !21! !!!!!!!!!!!!!else!if!(isObj(that._proxy[fn]))!{! !! !22! !!!!!!!!!!!!!!!!!rv!=!that._proxy[fn];! !! !23! !!!!!!!!!!!!!}! !! !24! !!!!!!!!!!!!!deferred.resolve(rv);! !! !25! !!!!!!!!!}).fail(function(){! !! !26! !!!!!!!!!!!!!deferred.reject('OnDemand!failed!to!load!script!'!!+!script);! !! !27! !!!!!!!!!});!!! !28! !!!!!}! !! !29! !!!!!return!deferred.promise();! !! !30! !};! !!4! !!!!!if!(isFn(that._proxy[fn]))!{! !! !!5! !!!!!!!!!rv!=!that._proxy[fn].apply(context,!args);! !!6! !!!!!!!!!deferred.resolve(rv);! !! !!7! !!!!!}! !! !!8! !!!!!else!if(isObj(that._proxy[fn]))!{! !! !!9! !!!!!!!!!deferred.resolve(that._proxy[fn]);!!! !10! !!!!!}!! !16! !!!!!!!!!$.getScript(script).done(function(){! !! !17! !!!!!!!!!!!!!var!rv;! !! !18! !!!!!!!!!!!!!if!(isFn(that._proxy[fn]))!{! !! !19! !!!!!!!!!!!!!!!rv!=!that._proxy[fn].apply(context,!args);!!! !20! !!!!!!!!!!!!!}! !! !21! !!!!!!!!!!!!!else!if!(isObj(that._proxy[fn]))!{! !! !22! !!!!!!!!!!!!!!!!!rv!=!that._proxy[fn];! !! !23! !!!!!!!!!!!!!}! !! !24! !!!!!!!!!!!!!deferred.resolve(rv);! !! !25! !!!!!!!!!})!

Slide 29

Slide 29 text

PERFORMANCE

Slide 30

Slide 30 text

“0.1 second is about the limit for having the user feel that the system is reacting instantaneously…” ― Nielsen, Usability Engineering

Slide 31

Slide 31 text

Let’s relax the definition of" on demand •  On demand is not strictly on click. •  On demand is right before we expect the code to be needed.

Slide 32

Slide 32 text

BEYOND MADNESS

Slide 33

Slide 33 text

window.performance !1! !//!returns!a!duration!in!milliseconds! !! !2! !function!getUserPerceivedLoadingTime()!{! !! !3! !!!return!!! !4! !!!!!window.performance.timing.loadEventStart!!! !5! !!!!!!:!window.performance.timing.navigationStart;! !! !6! !}; !! http://j.mp/w3c-window-performance

Slide 34

Slide 34 text

Q&A

Slide 35

Slide 35 text

DO IT. DO IT WISELY.

Slide 36

Slide 36 text

If you are interested in these slides and some other material related to this presentation just follow @mmarcon." Feedback: http://j.mp/rate-mmarcon

Slide 37

Slide 37 text

Thank you.