84
jQuery internals and front-end optimisation Artur Cistov - DrupalCamp Ballyvaughan 2010

Front-end optimisation & jQuery Internals

Embed Size (px)

Citation preview

Page 1: Front-end optimisation & jQuery Internals

jQuery internals and front­end optimisation

Artur Cistov ­ DrupalCamp Ballyvaughan 2010

Page 2: Front-end optimisation & jQuery Internals

Why bother?

500ms slower = 20% drop in traffic (Google)400ms slower = 5­9% drop in full­page traffic (Yahoo)100ms slower = 1% drop in sales (Amazon)

Source: http://www.slideshare.net/stoyan/yslow-20-presentation

Page 3: Front-end optimisation & jQuery Internals

Why bother?

Google added site speed as a factor to search ranking on April 9, 2010

Page 4: Front-end optimisation & jQuery Internals

Why bother?

 Less CPU power and memory than    on the desktop Slower connections 25Kb cache limit per file on iPhone

Source: http://yuiblog.com/blog/2008/02/06/iphone-cacheability/

Page 5: Front-end optimisation & jQuery Internals

280slides.com

Page 6: Front-end optimisation & jQuery Internals

Quake II GWT Port

Source: http://www.youtube.com/watch?v=XhMN0wlITLk

Page 7: Front-end optimisation & jQuery Internals

Quake II GWT Port

Source: http://code.google.com/p/quake2-gwt-port/wiki/FAQ

What browser features does this rely on?Just about every HTML5 buzzword you've heard forthe past year or so:

Canvas/WebGL: For obvious reasons<audio>: For sound<video>: For in­game videosWeb Sockets: For client­server communicationLocal Storage: For saving prefs. and saved games

Page 8: Front-end optimisation & jQuery Internals

Plan for this talk

Front­end optimisation jQuery under the hood jQuery optimisation Tools & Resources

Page 9: Front-end optimisation & jQuery Internals

Front­end Optimisation

1. Dependency loading2. Initial Rendering3. Post­load responsiveness

Page 10: Front-end optimisation & jQuery Internals

1. Dependency Loading

Total time needed to download all the page assets (images, stylesheets, scripts etc.)

Ideally, full download only happens once, later on assets are taken from cache

Page 11: Front-end optimisation & jQuery Internals

Full vs. Empty Cache

Page 12: Front-end optimisation & jQuery Internals

Dependency Loading Optimisation Techniques

Minimising HTTP Requests Minimising total filesize Maximising parallel downloads Addressing blocking behaviour

Page 13: Front-end optimisation & jQuery Internals

developer.yahoo.com/performance/

Page 14: Front-end optimisation & jQuery Internals

Minimising HTTP Requests

Combining multiple JS & CSS files, combining images into sprites

Avoiding requests alltogether with caching (Expires & ETag headers)

Page 15: Front-end optimisation & jQuery Internals

Image Sprite Examples

Page 16: Front-end optimisation & jQuery Internals

Filesize

Gzipping Minifying scripts & styles Compressing images

Page 17: Front-end optimisation & jQuery Internals

Maximising parallel downloads

Browsers have 2­6 simultaneous request limit per domain name. 

Subdomains are treated as different domains in this context

Page 18: Front-end optimisation & jQuery Internals

Maximising parallel downloads

LABjs ­ “all-purpose, on-demand JavaScript loader, capable of loading any JavaScript resource, from any location, into any page, at any time.”

Allows to load scripts in parallel http://labjs.com/

Page 19: Front-end optimisation & jQuery Internals

Statically loading scripts (blocking)

Source: http://blog.getify.com/2009/11/labjs-new-hotness-for-script-loading/

Page 20: Front-end optimisation & jQuery Internals

Dynamically loading scripts

Source: http://blog.getify.com/2009/11/labjs-new-hotness-for-script-loading/

Page 21: Front-end optimisation & jQuery Internals

Non­blocking loading: Google Analytics

Page 22: Front-end optimisation & jQuery Internals

2. Speeding Up Initial Page Rendering

Page 23: Front-end optimisation & jQuery Internals

Speeding up onload render ­ techniques

Assets order .js class trick Lazy Loading Banners & tracking scripts Flash of Unscripted Content

Page 24: Front-end optimisation & jQuery Internals

Assets Order

CSS at the top, JavaScript at the bottom Avoid @import for CSS

Page 25: Front-end optimisation & jQuery Internals

Lazy Loading Deferring loading of a component after 

page load Module loader coming in jQuery 1.5? Facebook Primer library

Page 26: Front-end optimisation & jQuery Internals

.js class trick

Page 27: Front-end optimisation & jQuery Internals

Performance of 3rd Party Content

Source: http://www.stevesouders.com/p3pc/

Page 28: Front-end optimisation & jQuery Internals

3rd Party Content

9 additional HTTP requests for Digg Widget, 107 kB total download size, 665 ms median load time

Page 29: Front-end optimisation & jQuery Internals

Flash of unscripted content problem

Elements are rendered, but their behaviour hasn't been assigned yet

Page 30: Front-end optimisation & jQuery Internals
Page 31: Front-end optimisation & jQuery Internals

Traditional Solution:Script immediately after element

Page 32: Front-end optimisation & jQuery Internals

One of the modern solutions:Google Analytics Approach

Page 33: Front-end optimisation & jQuery Internals

3. Post­load responsiveness

Page 34: Front-end optimisation & jQuery Internals

Source: http://ejohn.org/blog/javascript-performance-stack/

Many factors

Page 35: Front-end optimisation & jQuery Internals

Post­load responsiveness techniques

Minimising Reflows & RepaintsJavaScript Optimisation

Page 36: Front-end optimisation & jQuery Internals

Repaints

Occur when something made visible or hidden without altering the layout. 

E.g. outline added to an element, background color or visibility changed

Repainting is expensive.

Page 37: Front-end optimisation & jQuery Internals

Reflows

Reflow occurs when the DOM is manipulated in a way it affects the layout. E.g. style is changed to affect the layout, className property is changed or browser window is resized. 

Reflows are even more expensive than repainting.

Page 38: Front-end optimisation & jQuery Internals

Reflows

Source http://dev.opera.com/articles/view/efficient-javascript/?page=3#reflow

Reflows are very expensive in terms of performance, and is one of the main causes of slow DOM scripts, especially on devices with low processing power, such as phones. In many cases, they are equivalent to laying out the entire page again.

Page 39: Front-end optimisation & jQuery Internals

Reflows are triggered by

Style is changed that affects the layout className property of an element is changed DOM modifications (e.g. adding new 

elements) using offsetWidth / offsetHeight / 

getComputedStyle

Page 40: Front-end optimisation & jQuery Internals

Minimising reflows Batch style updates

Source http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/

Page 41: Front-end optimisation & jQuery Internals

Minimising reflows

Detaching element from the DOM, making changes & reinserting 

Hide element before changes, apply them & show again

innerHTML DOMDocumentFragment

Batch DOM changes and/or perform them off the DOM:

Page 42: Front-end optimisation & jQuery Internals

Minimising reflows

Source http://wiki.forum.nokia.com/index.php/JavaScript_Performance_Best_Practices#Use_createDocumentFragment.28.29

Page 43: Front-end optimisation & jQuery Internals

Minimising reflows

Apply animations with position fixed or absolute

Page 44: Front-end optimisation & jQuery Internals

Underlying Problem of Single Thread

UI rendering & JavaScript share the same thread of execution

Long­running scripts can freeze UI or cause 'Do you want to stop this script' prompts

Page 45: Front-end optimisation & jQuery Internals

Web Workers API

 Draft Recommendation — 12 May 2010  Background workers running scripts in   

parallel to the main page Message passing

Page 46: Front-end optimisation & jQuery Internals

Some JavaScript Optimisations

Variable lookup performance Avoiding eval Caching array length in loops Using try/catch sparingly

Page 47: Front-end optimisation & jQuery Internals

Front­end Optimisation Recap:

1. Dependency loading (HTTP requests, filesize, parallel downloads, blocking)

2. Rendering (Asset order, Lazy loading, Flash of unstyled content)

3. Post­load responsiveness (Reflows & repaints, JavaScript optimisations)

Page 48: Front-end optimisation & jQuery Internals
Page 49: Front-end optimisation & jQuery Internals

jQuery Usage

Source:http://trends.builtwith.com/javascript/JQuery

Page 50: Front-end optimisation & jQuery Internals

jQuery Usage

Source:http://trends.builtwith.com/javascript/JQuery

Page 51: Front-end optimisation & jQuery Internals

jQuery Performance

Source: http://www.flickr.com/photos/jeresig/4366089661/

Page 52: Front-end optimisation & jQuery Internals

jQuery Productivity

Source: http://www.slideshare.net/paul.irish/perfcompression

Page 53: Front-end optimisation & jQuery Internals

Presenting Barebones jQuery 0.1 Small subset of jQuery core Useful for learning about inner workings 

of jQuery 50 vs. 6325 lines in full jQuery, so a little 

less powerful :) http://github.com/cistov/Barebones­jQuery

Page 54: Front-end optimisation & jQuery Internals

Sample Usage

Page 55: Front-end optimisation & jQuery Internals

Full Source:

Page 56: Front-end optimisation & jQuery Internals

1. Initialisation

Page 57: Front-end optimisation & jQuery Internals

2. jQuery.prototype

Page 58: Front-end optimisation & jQuery Internals

3. Utility methods

Page 59: Front-end optimisation & jQuery Internals

4. Aliases

Page 60: Front-end optimisation & jQuery Internals

5. Sample Plug­ins

Page 61: Front-end optimisation & jQuery Internals

jQuery Instance (Matched/Wrapped Set)

Page 62: Front-end optimisation & jQuery Internals

Two fundamental pieces of functionality in jQuery

jQuery instance methods e.g. $('#nav a').show(); utility ('static') methods$.noConflict();

Page 63: Front-end optimisation & jQuery Internals

jQuery optimisation

Page 64: Front-end optimisation & jQuery Internals

Use the latest version 1.2.6 shipping with Drupal 6 1.4.2 shipping with Drupal 7

Page 65: Front-end optimisation & jQuery Internals

Sizzle selector engine

● Introduced in jQuery 1.3● http://sizzlejs.com/● Unlike earlier versions of jQuery, it parses selectors from right to left● This is how browsers parse CSS too

Page 66: Front-end optimisation & jQuery Internals

Specific on the right & generic on the left

Page 67: Front-end optimisation & jQuery Internals

Chain or cache selections

Page 68: Front-end optimisation & jQuery Internals

Don't act on empty sets

Page 69: Front-end optimisation & jQuery Internals

Class selectors

Page 70: Front-end optimisation & jQuery Internals

jQuery.fn.find

Page 71: Front-end optimisation & jQuery Internals

Events

Page 72: Front-end optimisation & jQuery Internals

Memory Leaks

Source: http://msdn.microsoft.com/en-us/library/bb250448%28VS.85%29.aspx

Page 73: Front-end optimisation & jQuery Internals

Memory Leaks

Avoid attaching objects to DOM nodes directly Use jQuery methods instead:

Page 74: Front-end optimisation & jQuery Internals

jQuery source viewerhttp://james.padolsey.com/jquery

Page 75: Front-end optimisation & jQuery Internals

jQuery: what's coming

Ajax module rewrite Dependency & load management Templating Data binding Mobile support

Page 76: Front-end optimisation & jQuery Internals

jQuery Dublinhttp://meetups.jquery.com/group/jquerydublin

Page 77: Front-end optimisation & jQuery Internals

Tools & Resources

Page 78: Front-end optimisation & jQuery Internals

Google Closure Compiler

Page 79: Front-end optimisation & jQuery Internals

Google Closure Compiler Open­source, written in Java & easy to extend Three modes  Up to 60­70% filesize savings Advanced code transforms based on syntax tree including constant & function inlining, dead code removal etc. Highlights code patterns that may not work well on all browsersjQuery gained 13% minification improvement by switiching to Google Compiler from YUI compressor

Page 80: Front-end optimisation & jQuery Internals

dynaTrace AJAX Editionhttp://ajax.dynatrace.com/

Page 81: Front-end optimisation & jQuery Internals

CuzillionOpen­source web performance exploration tool 

Page 82: Front-end optimisation & jQuery Internals

v

Books

Page 83: Front-end optimisation & jQuery Internals

Links

Yahoo Exceptional performance teamhttp://developer.yahoo.com/performance/

Nokia JavaScript Performance Best Practiceshttp://wiki.forum.nokia.com/index.php/JavaScript_Performance_Best_Practices

Google Performance resourceshttp://code.google.com/speed/

Steve Souders, ex Chief Performance Yahoohttp://stevesouders.com/

Page 84: Front-end optimisation & jQuery Internals

Thanks

http://www.slideshare.net/cistovhttp://www.twitter.com/cistov

http://github.com/cistov/