jQuery internals and frontend
optimisation
Artur Cistov – PyCon Ireland 2010
Why bother?
500ms slower = 20% drop in traffic (Google)
400ms slower = 59% drop in fullpage traffic (Yahoo)
100ms slower = 1% drop in sales (Amazon)
Source: http://www.slideshare.net/stoyan/yslow-20-presentation
Why bother?
Google added site speed as a factor to search
ranking on April 9, 2010
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/
280slides.com
Quake II GWT Port
Source: http://www.youtube.com/watch?v=XhMN0wlITLk
Plan for this talk
Frontend optimisation
jQuery under the hood
jQuery optimisation
Tools & Resources
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
Maximising parallel downloads
Browsers have 26 simultaneous request
limit per domain name.
Subdomains are treated as different
domains in this context
Maximising parallel downloads
LABjs “allpurpose, ondemand
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/
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.
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.
Reflows
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.
Source http://dev.opera.com/articles/view/efficient-javascript/?page=3#reflow
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
Minimising reflows
Batch DOM changes and/or perform them off the DOM:
Detaching element from the DOM, making changes
& reinserting
Hide element before changes, apply them & show
again
innerHTML
DOMDocumentFragment
Minimising reflows
Apply animations with position fixed or
absolute
Underlying Problem of Single
Thread
UI rendering & JavaScript share the
same thread of execution
Longrunning scripts can freeze UI or
cause 'Do you want to stop this script'
prompts
Web Workers API
Draft Recommendation — 12 May 2010
Background workers running scripts in
parallel to the main page
Message passing
Some JavaScript Optimisations
Variable lookup performance
Avoiding eval
Caching array length in loops
Using try/catch sparingly
Two fundamental pieces of
functionality in jQuery
jQuery instance methods
e.g. $('#nav a').show();
utility ('static') methods
$.noConflict();
jQuery optimisation
Use the latest version
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
Google Closure Compiler
Opensource, written in Java & easy to extend
Three modes
Up to 6070% 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 browsers
jQuery gained 13% minification improvement by
switiching to Google Compiler from YUI compressor
dynaTrace AJAX Edition
http://ajax.dynatrace.com/
Cuzillion
Opensource web performance exploration tool
Books
v
Links
Yahoo Exceptional performance team
ttp://developer.yahoo.com/performance/
Nokia JavaScript Performance Best Practices
http://wiki.forum.nokia.com/index.php/JavaScript_Performance_Best_Practices
Google Performance resources
http://code.google.com/speed/
Steve Souders, ex Chief Performance Yahoo
http://stevesouders.com/
1–1 of 1 previous next