Shifting Gears

130
Shifting your site into the next gear Christian Heilmann | http://wait-till-i.com | http://twitter.com/codepo8 GeekMeet, Stockholm, Sweden, December 2008 http://www.flickr.com/photos/fabiovenni/91780334/

description

My first talk at geekmeet Sweden, talking about basics of web site performance.

Transcript of Shifting Gears

Page 1: Shifting Gears

Shifting your site into the next gear

Christian Heilmann | http://wait-till-i.com | http://twitter.com/codepo8

GeekMeet, Stockholm, Sweden, December 2008

htt

p:/

/ww

w.f

lick

r.co

m/p

ho

tos/

fab

iove

nn

i/91

7803

34/

Page 2: Shifting Gears

Hello there...

Page 3: Shifting Gears

I’m Chris.

Page 4: Shifting Gears

It is great to be in Sweden...

Page 5: Shifting Gears
Page 6: Shifting Gears

...especially when you live in England...

Page 7: Shifting Gears
Page 8: Shifting Gears

Ok, I originally meant not to make any naughty jokes.

Page 9: Shifting Gears

But then I saw the Adwords Robert seems to have bought

for GeekMeet.

Page 10: Shifting Gears
Page 11: Shifting Gears

... enough of this, let’s get serious ...

Page 12: Shifting Gears

The performance of your web product is immensely important

Page 13: Shifting Gears

And there are several small tricks and ideas that can

increase the performance significantly.

Page 14: Shifting Gears

There is a lot of research being done on the subject in

a lot of companies...

Page 15: Shifting Gears

...and this information is available for free for you.

Page 16: Shifting Gears

I’ll provide the links and names later...

Page 17: Shifting Gears

...but for now let’s see what is slowing our sites down.

Page 18: Shifting Gears

Things that slow you down

http://www.flickr.com/photos/pikerslanefarm/2428232674/

Page 19: Shifting Gears

<script>

Page 20: Shifting Gears

JavaScript is awesome.

Page 21: Shifting Gears

Browsers think so, too.

Page 22: Shifting Gears

Which means that every time they encounter a script node

they take a break.

<script> </script>

Page 23: Shifting Gears

Rendering stops and the JavaScript parser takes over.

Page 24: Shifting Gears

If there is code inside the tag, this code is analyzed and

executed before the browser goes back to rendering.

Page 25: Shifting Gears

If there is a src attribute, the file it points to gets loaded,

parsed and executed.

Page 26: Shifting Gears

This can take time as the browser (with third party

files) needs to get the right domain information, contact the other server, get the file

and then move on.

Page 27: Shifting Gears

This gets worse the more script nodes you use.

Page 28: Shifting Gears

The first question is then where to put scripts?

Page 29: Shifting Gears

The gospel according to Heilmann, 2006:

Page 30: Shifting Gears

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="se-se(bork bork)"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript" src="myscripts.js"></script></head><body><!-- lots of HTML here --></body></html>

Page 31: Shifting Gears

This has some benefits:

Page 32: Shifting Gears

You can be sure your JavaScript is loaded before the document is displayed.

Page 33: Shifting Gears

You make it easier for other developers to debug – they know where the scripts are.

Page 34: Shifting Gears

On the other hand...

Page 35: Shifting Gears

You delay the display of the page until all the scripts are

loaded and executed.

Page 36: Shifting Gears

You need to delay the access to any HTML to change or

enhance until it is available – onload, onavailable, oncontentloaded.

Page 37: Shifting Gears

This is hacky!

Page 38: Shifting Gears

The gospel according to performance specialists

2007/2008:

Page 39: Shifting Gears

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="se-se(bork bork)"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title></head><body><!-- lots of HTML here --> <script type="text/javascript" src="myscripts.js"></script></body></html>

Page 40: Shifting Gears

The benefits are:

Page 41: Shifting Gears

Your scripts don’t delay the rendering of the HTML.

Page 42: Shifting Gears

HTML is available for enhancing.

Page 43: Shifting Gears

The problems:

Page 44: Shifting Gears

You don’t put scripts where people normally expect

them.

Page 45: Shifting Gears

The HTML is already available for people to interact with before you can apply your

enhancement voodoo.

Page 46: Shifting Gears

The recipe for performance and usability success?

Page 47: Shifting Gears

Of course you need testing on your apps and sites to find

your perfect solution...

Page 48: Shifting Gears

But here are some ideas:

Page 49: Shifting Gears

How about a mix of both?

Page 50: Shifting Gears

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="se-se(bork bork)"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript" src="main.js"></script></head><body><!-- lots of HTML here --> <script type="text/javascript" src="secondary.js"></script></body></html>

Page 51: Shifting Gears

You can even execute delayed without hacks.

Page 52: Shifting Gears

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="se-se(bork bork)"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript" src="main.js"></script></head><body><!-- lots of HTML here --> <script type="text/javascript">doStuff()</script> <script type="text/javascript" src="secondary.js"></script></body></html>

Page 53: Shifting Gears

The first script can set a class called js on the body of the

document.

Page 54: Shifting Gears

Which allows you to define two styles, one for the non-

scripting version and one for the dynamic one.

Page 55: Shifting Gears

In the dynamic one you hide the problematic elements

that could be activated prematurely.

Page 56: Shifting Gears

Once the functionality has been added, add another

class that undoes this.

Page 57: Shifting Gears

.js #buttons{ position:absolute; top:0; left:-6000px;}.jsloaded #buttons{ position:relative; top:0; left:0;}

Page 58: Shifting Gears

CSS parsers are very fast!

Page 59: Shifting Gears

What about the multiple scripts issue?

Page 60: Shifting Gears

Chunking your JavaScript into several includes all doing one job at a time is a great idea to

keep your solutions maintainable.

Page 61: Shifting Gears

<script type="text/javascript" src="config.js"></script><script type="text/javascript" src="base.js"></script><script type="text/javascript" src="effects.js"></script><script type="text/javascript" src="validation.js"></script><script type="text/javascript" src="widgets.js"></script>

Page 62: Shifting Gears

This does not mean though that you need to add all of

them as single includes.

Page 63: Shifting Gears

You can easily write a backend script that does that

for you:

Page 64: Shifting Gears

<script type="text/javascript" src="/pack/config/base/effects/validation/widgets"></script>

Page 65: Shifting Gears

Fun things to do in this script:

Page 66: Shifting Gears

Run through JSLint.

Page 67: Shifting Gears

Minify.

Page 68: Shifting Gears

Replace all strings with an array lookup to stop IE from creating a string object for

each string.

Page 69: Shifting Gears

Instead of doing this every time the page is loaded, you

can make it part of a build process.

Page 70: Shifting Gears

And don’t forget to cache the result!

Page 71: Shifting Gears

Get a script that does most of that from Ed Eliot:

http://www.ejeliot.com/blog/73

Page 72: Shifting Gears

The other option is to be lazy.

Page 73: Shifting Gears

Lazy loading is a concept to load dependencies only when

and if they are needed.

Page 74: Shifting Gears

Your base.js could test for all the things that should

work...

Page 75: Shifting Gears

...and if they do, create script nodes dynamically to load the other, chunkier parts.

Page 76: Shifting Gears

This is a pretty nice idea as it only loads things when they

are needed...

Page 77: Shifting Gears

...thus saving server traffic and diminishing loading time.

Page 78: Shifting Gears

However, there are problems.

Page 79: Shifting Gears

Asynchronous loading means you never know in which

order the loaded components come back to you...

Page 81: Shifting Gears

There are a lot more issues, for example that you might block document.write() of bad code ads in your page.

Page 83: Shifting Gears

What other things slow you down?

Page 84: Shifting Gears

<img>

Page 85: Shifting Gears

Images are what made the web what it is now.

Page 86: Shifting Gears

Sure, linking text was a revolution.

Page 87: Shifting Gears

But being able to get images on demand and store them

was something that triggered the “collector” in everyone.

Page 88: Shifting Gears

Yeah, Pr0n.

Page 89: Shifting Gears

Anyways, too many images slow down your site

immensely.

Page 90: Shifting Gears

The reasons are the same delays you experience with

external scripts...

Page 91: Shifting Gears

...except images don’t stop the rendering.

Page 92: Shifting Gears

However only a few get loaded at a time...

Page 93: Shifting Gears

...as the number of parallel connections of browsers is limited (with good reason)

Page 94: Shifting Gears

Which means you should cut down on the amount of

images.

Page 95: Shifting Gears

The best trick there is to use CSS sprites.

Page 96: Shifting Gears

And again, Ed Eliot and Stuart Colville come to the rescue!

http://spritegen.website-performance.org/

Page 97: Shifting Gears

You use Spritegen by uploading a zip of images...

Page 98: Shifting Gears

...and it generates the CSS and the sprite image for you!

Page 99: Shifting Gears

It is also open source in case you want to host it yourself.

Page 100: Shifting Gears

How good are you in optimizing images?

Page 101: Shifting Gears

Probably not as good as you think you are.

Page 102: Shifting Gears

A lot of bytes can be squeezed out of images...

Page 103: Shifting Gears

...without changing their visual outcome.

Page 104: Shifting Gears

Choosing the right format in the right version can be quite

a drag.

Page 105: Shifting Gears

And it doesn’t help that tools put all kind of pointless data into images (edited with xyz,

version abc...).

Page 106: Shifting Gears

The easy way out?

Page 107: Shifting Gears

http://smushit.com

Page 108: Shifting Gears
Page 109: Shifting Gears

What else could be a problem?

Page 110: Shifting Gears

How about the amount of images on the page?

Page 111: Shifting Gears

You can work around loading all the images by – once

again – delaying the loading.

Page 112: Shifting Gears

The easy way is to prepend the images with a dummy

and then replace this.

Page 113: Shifting Gears

<img src=”dummy.gif#kitten.jpg” alt=”a nice kitten”><img src=”dummy.gif#puppy.jpg” alt=”a nice puppy”>

<script type="text/javascript">window.onload = function(){

var imgs = document.getElementsByTagName(‘img’);for(var i=0;imgs[i];i++){var src = imgs[i].src;imgs[i].src = src.replace(‘dummy.gif#’,’’);

}}</script>

Page 114: Shifting Gears

The more complex but cleverer way is to wait until

the images are in the viewport.

http://developer.yahoo.com/yui/imageloader/

Page 115: Shifting Gears

You can see this trick in action at shine.yahoo.com

and YouTube.

Page 116: Shifting Gears

It’d be cool if browsers did that anyways – anyone

remember lowsrc?

Page 117: Shifting Gears

What else is slowing us down?

Page 118: Shifting Gears

<object>, err <embed> err...

Page 119: Shifting Gears

Flash and other plugins have the same issues as <script>

has.

Page 120: Shifting Gears

You interfere with the browsers’ normal rendering

and they wait until the confusion is over.

Page 121: Shifting Gears

The other issue of course is that making embedding work

and validate is a pain.

Page 122: Shifting Gears

The solution for this is progressively enhancing

Flash embedding.

Page 123: Shifting Gears

http://swfobject.googlecode.com

Page 124: Shifting Gears

As you embed with JavaScript you can test for the correct

Flash support.

Page 125: Shifting Gears

And you can delay the embedding by for example enhancing an image when

the user clicks on it.

Page 126: Shifting Gears

SUMMARY TIME!

Page 127: Shifting Gears

In general there are two things to do:

Page 128: Shifting Gears

Collate and Delay :)

Page 129: Shifting Gears

The tips and tools:

★ http://developer.yahoo.com/performance/★ http://www.stevesouders.com/★ http://website-performance.org/

★ YSlow: http://developer.yahoo.com/yslow/ ★ Hammerhead: http://stevesouders.com/hammerhead/

Page 130: Shifting Gears

Christian Heilmann

http://scriptingenabled.org | http://wait-till-i.com

twitter/flickr: codepo8

THANSK!Svenska?