please don't touch the slow parts v3

85
Please Don’t Touch the Slow Parts V3 [email protected] http://www.ideato.it/ [email protected] http://federico.galassi.net /

Upload: federico-galassi

Post on 17-May-2015

1.725 views

Category:

Technology


0 download

DESCRIPTION

"Please don't touch the slow parts" version 3 given by me at From The Front, Camp Nou Edition on March 18th 2011 in Modena. It's about improving web applications speed. Based on Steve Souders work. Compared to V2, I improved and expanded on how browsers work and how to optimize dom/css Also http://www.ideato.it/ http://federico.galassi.net/ http://www.stevesouders.com/ http://www.bettersoftware.it/ http://www.phpday.it/ http://fromthefront.it/ Follow me on Twitter! https://twitter.com/federicogalassi

TRANSCRIPT

Page 3: Please Don't Touch the Slow Parts V3
Page 4: Please Don't Touch the Slow Parts V3

faster

Page 5: Please Don't Touch the Slow Parts V3

faster WEB

Page 6: Please Don't Touch the Slow Parts V3

Faster == Better?

Page 7: Please Don't Touch the Slow Parts V3

We have to wait

Page 8: Please Don't Touch the Slow Parts V3

... All the time

Page 9: Please Don't Touch the Slow Parts V3

“Savings in timefeels like simplicity”

Page 10: Please Don't Touch the Slow Parts V3

“Time is the only commodity that matters”

Page 11: Please Don't Touch the Slow Parts V3

Psychology of webperformance

http://www.websiteoptimization.com/speed/tweak/psychology-web-performance/

5-8SECONDS

Page 12: Please Don't Touch the Slow Parts V3

Faster web, more clicks

http://www.stevesouders.com/blog/2009/07/27/wikia-fast-pages-retain-users/

Page 13: Please Don't Touch the Slow Parts V3

Faster web, better SEO

http://googlewebmastercentral.blogspot.com/2010/04/using-site-speed-in-web-search-ranking.html

Page 14: Please Don't Touch the Slow Parts V3

Faster web is hot

Page 15: Please Don't Touch the Slow Parts V3

Say web, Say browser

Page 16: Please Don't Touch the Slow Parts V3

How browsers work

Page 17: Please Don't Touch the Slow Parts V3

User clicks on a link

Page 18: Please Don't Touch the Slow Parts V3

Browser resolves domain name

www.google.com

72.14.234.104

DNS

UDP

domain

Page 19: Please Don't Touch the Slow Parts V3

Browser connects to web server

TCP/IP

72.14.234.104

WEB

domain connect

Page 20: Please Don't Touch the Slow Parts V3

Browser sends arequest for a page

HTTP

GET /language_tools?hl=enHost: www.google.com

domain connect send

WEB

Page 21: Please Don't Touch the Slow Parts V3

Browser receives a response with the page

HTTP

200 OK

domain connect send receive

WEB

Page 22: Please Don't Touch the Slow Parts V3

Browser renders the new page

domain connect send receive render

Page 23: Please Don't Touch the Slow Parts V3

Rendering is complexrender

Page 24: Please Don't Touch the Slow Parts V3

Rendering isloading resources

render

css

img

css

img

javascript

javascript

flash

Page 25: Please Don't Touch the Slow Parts V3

Each resource is another web request

render

Page 26: Please Don't Touch the Slow Parts V3

Requests areprocessed in parallel

render

Page 27: Please Don't Touch the Slow Parts V3

Rendering is parsingrender

HTML

CSS

DOM TREE

STYLE STRUCT

<html> <head> <title>Title</title> </head> <body> <div>This is a Text</div> <div id="hidden">Hidden</div>

body { padding: 0;}#hidden { display: none;}

- document - elem: html - elem: head - elem: title - text: Title - elem: body - elem: div - text: This is a Text - elem: div - attr: id=hidden - text: Hidden

- selector: body rule: display: block # default css padding-bottom: 0px # site css padding-left: 0px # site css padding-right: 0px # site css padding-top: 0px # site css- selector: hidden rule: display: none # site css

Page 28: Please Don't Touch the Slow Parts V3

Rendering is layoutrender

DOM TREE

STYLE STRUCT

- document - elem: html - elem: head - elem: title - text: Title - elem: body - elem: div - text: This is a Text - elem: div - attr: id=hidden - text: Hidden

- selector: body rule: display: block # default css padding-bottom: 0px # site css padding-left: 0px # site css padding-right: 0px # site css padding-top: 0px # fsite css- selector: hidden rule: display: none # site css

- root - body - block - inline: This is - inline: a Text

RENDER TREE

reflow

Page 29: Please Don't Touch the Slow Parts V3

Rendering is paintingrender

- root - body - block - inline: This is - inline: a Text

RENDER TREEThis is

a Text

repaint

Page 30: Please Don't Touch the Slow Parts V3

Rendering is execution

mouse moved mouse pressed mouse released key pressed key released

render

INPUT

OS

EVENT QUEUE

Page 31: Please Don't Touch the Slow Parts V3

Execution in one threadmouse moved

mouse pressed mouse released key pressed key released

render

EVENT QUEUE

Javascript Execution

NativeBrowserAction

Page 32: Please Don't Touch the Slow Parts V3

Once upon a time...

Few resources

Static pages

Less javascript

Page 33: Please Don't Touch the Slow Parts V3

Most time on serverdomain connect send receive render

Page 34: Please Don't Touch the Slow Parts V3

Solution is faster serversidedomain connect send receive render

Page 35: Please Don't Touch the Slow Parts V3

Ajax revolution

Page 36: Please Don't Touch the Slow Parts V3

Ajax revolutionperformance

Page 37: Please Don't Touch the Slow Parts V3

Page updating

One time(classic) WEB

Page 38: Please Don't Touch the Slow Parts V3

On demand(ajax) WEB

... later ...

Page updating

Page 39: Please Don't Touch the Slow Parts V3

Continuous(polling) WEB

Page updating

Page 40: Please Don't Touch the Slow Parts V3

Push(comet) WEB

Page updating

Page 41: Please Don't Touch the Slow Parts V3

Most time on browserdomain connect send receive render

Page 42: Please Don't Touch the Slow Parts V3

Golden rule of faster web

80% of the end user response time is spent

on the front-end

Page 43: Please Don't Touch the Slow Parts V3

Golden rule of faster web

Start there.

Page 44: Please Don't Touch the Slow Parts V3

Why webslow parts?

Page 45: Please Don't Touch the Slow Parts V3

Easy to understand

Page 46: Please Don't Touch the Slow Parts V3

Each part has its rules

Page 47: Please Don't Touch the Slow Parts V3

Which parts are slow?

Page 48: Please Don't Touch the Slow Parts V3

Network is slow

Page 49: Please Don't Touch the Slow Parts V3

Less stuffFewer requests

Concatenate js/cssCss spritesInline images

Too many resources

Page 50: Please Don't Touch the Slow Parts V3

Less stuffCache requests

Expires headerRevving FilesExternal js/cssRemove etags

Resourcesre-downloaded

Page 51: Please Don't Touch the Slow Parts V3

Smaller stuffCompress responses

Content-EncodingGzipDeflate

Resources are too big

Page 52: Please Don't Touch the Slow Parts V3

Smaller stuffMinify responses

js, css, htmlremove formattingremove commentsoptimize imagesuse tools

Resources are too big

Page 53: Please Don't Touch the Slow Parts V3

Closer stuffUse a CDN

Resources are too far

reduce latency

Page 54: Please Don't Touch the Slow Parts V3

Closer stuffFlush document early

Server can be slow

Chunked encoding

Page 55: Please Don't Touch the Slow Parts V3

Browser is slow

Page 56: Please Don't Touch the Slow Parts V3

Scripts block loading

javascript

css

javascript

img

flash

document.writelocation.hrefscripts order

html

img

Page 57: Please Don't Touch the Slow Parts V3

css

Put scripts at bottom

javascript

javascript

img

img

flash

html

Page 58: Please Don't Touch the Slow Parts V3

css

Unloaded stylesblock page rendering

html

img

img

flash

Page 59: Please Don't Touch the Slow Parts V3

css

html

img

img

flash

Put styles at top

Page 60: Please Don't Touch the Slow Parts V3

Indeed ... scripts block everything

Page 61: Please Don't Touch the Slow Parts V3

Load scripts asynchronously

var scriptTag = document.createElement("script")scriptTag.src = "http://www.example.org/js/lib.js"

document.getElementsByTagName("head")[0] .appendChild(scriptTag)

Page 62: Please Don't Touch the Slow Parts V3

Yield with timers

// doSomethingLong() is too slow, split it

doSomething()

setTimeout(function() { doSomethingElse()}, 50)

Page 63: Please Don't Touch the Slow Parts V3

Browser I/Ois slow

Page 64: Please Don't Touch the Slow Parts V3

Browser I/Ois slow

DOM

DoG

Page 65: Please Don't Touch the Slow Parts V3

is aliveDOM

Collections to arraysCache values to variables

DOM access triggers a live query

Page 66: Please Don't Touch the Slow Parts V3

triggers eventsDOM

Event Delegation

Events execute JS code

Page 67: Please Don't Touch the Slow Parts V3

Reflow is expensiveBatch DOMchanges “offline”

Cloned elementDocument FragmentDisplay: none

http://www.youtube.com/watch?v=ZTnIxIA5KGw

Page 68: Please Don't Touch the Slow Parts V3

Reflow is expensiveBatch CSSchanges

One class to rule em allDynamic style property

http://www.youtube.com/watch?v=ZTnIxIA5KGw

Page 69: Please Don't Touch the Slow Parts V3

Inefficient element location

CSS are bottom-up!

Be specific on the “right”

#header li a direction

Page 70: Please Don't Touch the Slow Parts V3

Inefficient element location

Go native in DOM

getElementByIdXpathquerySelectorAll

Page 71: Please Don't Touch the Slow Parts V3

Rules pitfalls

Page 73: Please Don't Touch the Slow Parts V3

Expect the unexpected

empty cacheno compression

Page 74: Please Don't Touch the Slow Parts V3

Know your users

Track user capabilities

Page 75: Please Don't Touch the Slow Parts V3

Conflicting rules

DNS vs Parallel

Inline vs External

Concatenated vs Reuse

Page 76: Please Don't Touch the Slow Parts V3

All that glittersis not gold

Page 77: Please Don't Touch the Slow Parts V3

Everything is atradeoff

Page 78: Please Don't Touch the Slow Parts V3

performance bringscomplexity

Page 79: Please Don't Touch the Slow Parts V3

know the rules but...

Page 80: Please Don't Touch the Slow Parts V3

leave complexityto computers

Page 81: Please Don't Touch the Slow Parts V3

use librariesduring development

Page 82: Please Don't Touch the Slow Parts V3

Use toolsat build time

http://code.google.com/speed/tools.html

Page 83: Please Don't Touch the Slow Parts V3

Code smartat run time

http://www.slideshare.net/ajaxexperience2009/david-wei-and-changhao-jiang-presentation

AdaptiveOptimization

Page 85: Please Don't Touch the Slow Parts V3

Questions?