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

Please, dont touch the slow parts v.3.6 @webtechcon

Embed Size (px)

Citation preview

Page 1: Please, dont touch the slow parts v.3.6 @webtechcon

Please Don’t Touch the Slow Parts V3.6

[email protected]://www.ideato.it/

[email protected]://federico.galassi.net/

Page 2: Please, dont touch the slow parts v.3.6 @webtechcon
Page 3: Please, dont touch the slow parts v.3.6 @webtechcon

faster

Page 4: Please, dont touch the slow parts v.3.6 @webtechcon

WEBfaster

Page 5: Please, dont touch the slow parts v.3.6 @webtechcon

Faster == Better?

Page 6: Please, dont touch the slow parts v.3.6 @webtechcon

We have to wait

Page 7: Please, dont touch the slow parts v.3.6 @webtechcon

... All the time

Page 8: Please, dont touch the slow parts v.3.6 @webtechcon

“Savings in timefeels like simplicity”

Page 9: Please, dont touch the slow parts v.3.6 @webtechcon

“Time is the only commodity that matters”

Page 10: Please, dont touch the slow parts v.3.6 @webtechcon

Psychology of webperformance

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

5-8SECONDS

Page 11: Please, dont touch the slow parts v.3.6 @webtechcon

Faster web, more clicks

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

Page 12: Please, dont touch the slow parts v.3.6 @webtechcon

Faster web, better SEO

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

Page 13: Please, dont touch the slow parts v.3.6 @webtechcon

Faster web is hot

Page 14: Please, dont touch the slow parts v.3.6 @webtechcon

Say web, Say browser

Page 15: Please, dont touch the slow parts v.3.6 @webtechcon

How browsers work

Page 16: Please, dont touch the slow parts v.3.6 @webtechcon

User clicks on a link

Page 17: Please, dont touch the slow parts v.3.6 @webtechcon

Browser resolves domain name

www.google.com

72.14.234.104

DNS

UDP

domaindomain

Page 18: Please, dont touch the slow parts v.3.6 @webtechcon

Browser connects to web server

TCP/IP

72.14.234.104

WEB

domaindomain connectconnect

Page 19: Please, dont touch the slow parts v.3.6 @webtechcon

Browser sends arequest for a page

HTTP

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

domaindomain connectconnect sendsend

WEB

Page 20: Please, dont touch the slow parts v.3.6 @webtechcon

Browser receives a response with the page

HTTP

200 OK

domaindomain connectconnect sendsend receivereceive

WEB

Page 21: Please, dont touch the slow parts v.3.6 @webtechcon

Browser renders the new page

domaindomain connectconnect sendsend receivereceive renderrender

Page 22: Please, dont touch the slow parts v.3.6 @webtechcon

Rendering is complexrenderrender

Page 23: Please, dont touch the slow parts v.3.6 @webtechcon

Rendering isloading resources

renderrender

csscss

imgimg

csscss

imgimg

javascriptjavascript

javascriptjavascript

flashflash

Page 24: Please, dont touch the slow parts v.3.6 @webtechcon

Each resource is another web request

renderrender

Page 25: Please, dont touch the slow parts v.3.6 @webtechcon

Requests areprocessed in parallel

renderrender

Page 26: Please, dont touch the slow parts v.3.6 @webtechcon

Rendering is parsingrenderrender

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 27: Please, dont touch the slow parts v.3.6 @webtechcon

Rendering is layoutrenderrender

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 28: Please, dont touch the slow parts v.3.6 @webtechcon

Rendering is paintingrenderrender

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

RENDER TREEThis is

a Text

repaint

Page 29: Please, dont touch the slow parts v.3.6 @webtechcon

Rendering is execution

mouse movedmouse moved mouse pressedmouse pressed mouse releasedmouse released key pressedkey pressed key releasedkey released

renderrender

INPUT

OS

EVENT QUEUE

Page 30: Please, dont touch the slow parts v.3.6 @webtechcon

Execution in one thread

mouse movedmouse moved mouse pressedmouse pressed mouse releasedmouse released key pressedkey pressed key releasedkey released

renderrender

EVENT QUEUE

JavascriptJavascript

ExecutionExecution

NativeNativeBrowserBrowserActionAction

Page 31: Please, dont touch the slow parts v.3.6 @webtechcon

Once upon a time...

Few resources

Static pages

Less javascript

Page 32: Please, dont touch the slow parts v.3.6 @webtechcon

Most time on serverdomaindomain connectconnect sendsend receivereceive renderrender

Page 33: Please, dont touch the slow parts v.3.6 @webtechcon

Solution is faster serverside

domaindomain connectconnect sendsend receivereceive renderrender

Page 34: Please, dont touch the slow parts v.3.6 @webtechcon

Ajax revolution

Page 35: Please, dont touch the slow parts v.3.6 @webtechcon

Ajax revolution

performance

Page 36: Please, dont touch the slow parts v.3.6 @webtechcon

Page updating

One time(classic) WEB

Page 37: Please, dont touch the slow parts v.3.6 @webtechcon

On demand(ajax) WEB

... later ...

Page updating

Page 38: Please, dont touch the slow parts v.3.6 @webtechcon

Continuous(polling) WEB

Page updating

Page 39: Please, dont touch the slow parts v.3.6 @webtechcon

Push(comet) WEB

Page updating

Page 40: Please, dont touch the slow parts v.3.6 @webtechcon

Most time on browserdomaindomain connectconnect sendsend receivereceive renderrender

Page 41: Please, dont touch the slow parts v.3.6 @webtechcon

Golden rule of faster web

80% of the end user response time is spent

on the front-end

Page 42: Please, dont touch the slow parts v.3.6 @webtechcon

Start there.

Golden rules of faster web

Page 43: Please, dont touch the slow parts v.3.6 @webtechcon

Why webslow parts?

Page 44: Please, dont touch the slow parts v.3.6 @webtechcon

Easy to understand

Page 45: Please, dont touch the slow parts v.3.6 @webtechcon

Each part has its rules

Page 46: Please, dont touch the slow parts v.3.6 @webtechcon

Which parts are slow?

Page 47: Please, dont touch the slow parts v.3.6 @webtechcon

Network is slow

Page 48: Please, dont touch the slow parts v.3.6 @webtechcon

Less stuffFewer requests

Concatenate js/cssCss spritesInline images

Too many resources

Page 49: Please, dont touch the slow parts v.3.6 @webtechcon

Less stuffCache requests

Expires headerRevving FilesExternal js/cssRemove etags

Resourcesre-downloaded

Page 50: Please, dont touch the slow parts v.3.6 @webtechcon

Smaller stuffCompress responses

Content-EncodingGzipDeflate

Resources are too big

Page 51: Please, dont touch the slow parts v.3.6 @webtechcon

Smaller stuffMinify responses

js, css, htmlremove formattingremove commentsoptimize imagesuse tools

Resources are too big

Page 52: Please, dont touch the slow parts v.3.6 @webtechcon

Closer stuffUse a CDN

Resources are too far

reduce latency

Page 53: Please, dont touch the slow parts v.3.6 @webtechcon

Closer stuffFlush document

early

Server can be slow

Chunked encoding

Page 54: Please, dont touch the slow parts v.3.6 @webtechcon

Browser is slow

Page 55: Please, dont touch the slow parts v.3.6 @webtechcon

Scripts block loading

javascriptjavascript

csscss

javascriptjavascript

imgimg

flashflash

document.writelocation.hrefscripts order

htmlhtml

imgimg

Page 56: Please, dont touch the slow parts v.3.6 @webtechcon

csscss

Put scripts at bottom

javascriptjavascript

javascriptjavascript

imgimg

imgimg

flashflash

htmlhtml

Page 57: Please, dont touch the slow parts v.3.6 @webtechcon

csscss

Unloaded stylesblock page rendering

htmlhtml

imgimg

imgimg

flashflash

Page 58: Please, dont touch the slow parts v.3.6 @webtechcon

csscss

htmlhtml

imgimg

imgimg

flashflash

Put styles at top

Page 59: Please, dont touch the slow parts v.3.6 @webtechcon

Indeed... scripts block everything

Page 60: Please, dont touch the slow parts v.3.6 @webtechcon

Load scripts asynchronously

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

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

Page 61: Please, dont touch the slow parts v.3.6 @webtechcon

Yield with timers

// doSomethingLong() is too slow, split it

doSomething()

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

Page 62: Please, dont touch the slow parts v.3.6 @webtechcon

Browser I/Ois slow

Page 63: Please, dont touch the slow parts v.3.6 @webtechcon

Browser I/Ois slow

DOM

DoG

Page 64: Please, dont touch the slow parts v.3.6 @webtechcon

is alive

Collections to arraysCache values to variables

DOM access triggers a live query

DOM

Page 65: Please, dont touch the slow parts v.3.6 @webtechcon

triggers events

Event Delegation

Events execute JS code

DOM

Page 66: Please, dont touch the slow parts v.3.6 @webtechcon

Reflow is expensive

Batch DOMchanges “offline”

Cloned elementDocument FragmentDisplay: none

Page 67: Please, dont touch the slow parts v.3.6 @webtechcon

Reflow is expensive

Batch CSS changes

One class to rule em allDynamic style property

Page 68: Please, dont touch the slow parts v.3.6 @webtechcon

Inefficient element location

CSS are bottom-up!

Be specific on the “right”

#header li a direction

Page 69: Please, dont touch the slow parts v.3.6 @webtechcon

Inefficient element location

Go native in DOM

getElementByIdXpathquerySelectorAll

Page 70: Please, dont touch the slow parts v.3.6 @webtechcon

Rules pitfalls

Page 71: Please, dont touch the slow parts v.3.6 @webtechcon

Panta reiBrowserscope

http://www.browserscope.org/

Page 72: Please, dont touch the slow parts v.3.6 @webtechcon

Expect the unexpected

empty cacheno compression

Page 73: Please, dont touch the slow parts v.3.6 @webtechcon

Know your usersTrack user capabilities

Page 74: Please, dont touch the slow parts v.3.6 @webtechcon

Conflicting rulesDNS vs ParallelInline vs ExternalConcatenated vs Reuse

Page 75: Please, dont touch the slow parts v.3.6 @webtechcon

All that glittersis not gold

Page 76: Please, dont touch the slow parts v.3.6 @webtechcon

Everything is atradeoff

Page 77: Please, dont touch the slow parts v.3.6 @webtechcon

performance bringscomplexity

Page 78: Please, dont touch the slow parts v.3.6 @webtechcon

know the rules but...

Page 79: Please, dont touch the slow parts v.3.6 @webtechcon

leave complexityto computers

Page 80: Please, dont touch the slow parts v.3.6 @webtechcon

use librariesduring development

Page 81: Please, dont touch the slow parts v.3.6 @webtechcon

Use toolsat build time

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

Page 82: Please, dont touch the slow parts v.3.6 @webtechcon

Use toolsat build time

Apache mod_pagespeed

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

Page 83: Please, dont touch the slow parts v.3.6 @webtechcon

Code smartat run time

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

AdaptiveOptimization

Page 84: Please, dont touch the slow parts v.3.6 @webtechcon

http://abetterbrowser.org/

Page 85: Please, dont touch the slow parts v.3.6 @webtechcon

Questions?