SlideShare une entreprise Scribd logo
1  sur  84
Télécharger pour lire hors ligne
jQuery internals and front­end 
            optimisation




    Artur Cistov ­ DrupalCamp Ballyvaughan 2010
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
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
Quake II GWT Port

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

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




                            Source: http://code.google.com/p/quake2-gwt-port/wiki/FAQ
Plan for this talk

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

1. Dependency loading
2. Initial Rendering
3. Post­load responsiveness
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
Full vs. Empty Cache
Dependency Loading Optimisation 
          Techniques

  Minimising HTTP Requests

  Minimising total filesize

  Maximising parallel downloads

  Addressing blocking behaviour
developer.yahoo.com/performance/
Minimising HTTP Requests


  Combining multiple JS & CSS files, 
  combining images into sprites

  Avoiding requests alltogether with 
  caching (Expires & ETag headers)
Image Sprite Examples
Filesize


  Gzipping

  Minifying scripts & styles

  Compressing images
Maximising parallel downloads


  Browsers have 2­6 simultaneous request 
  limit per domain name. 

  Subdomains are treated as different 
  domains in this context
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/
Statically loading scripts 
        (blocking)




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




          Source: http://blog.getify.com/2009/11/labjs-new-hotness-for-script-loading/
Non­blocking loading: 
  Google Analytics
2. Speeding Up Initial Page 
        Rendering
Speeding up onload render ­ 
            techniques

  Assets order

  .js class trick

  Lazy Loading

  Banners & tracking scripts

  Flash of Unscripted Content
Assets Order


  CSS at the top, JavaScript at the bottom

  Avoid @import for CSS
Lazy Loading

  Deferring loading of a component after 
  page load

  Module loader coming in jQuery 1.5?

  Facebook Primer library
.js class trick
Performance of 3rd Party Content




                     Source: http://www.stevesouders.com/p3pc/
3rd Party Content


    9 additional HTTP requests for Digg 
    Widget, 107 kB total download size, 665 
    ms median load time
Flash of unscripted content problem



    Elements are rendered, but their 
    behaviour hasn't been assigned yet
Traditional Solution:
Script immediately after element
One of the modern solutions:
     Google Analytics Approach
3. Post­load responsiveness
Many factors




       Source: http://ejohn.org/blog/javascript-performance-stack/
Post­load responsiveness 
            techniques


  Minimising Reflows & Repaints

  JavaScript Optimisation
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 style updates




                          Source http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/
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




Source http://wiki.forum.nokia.com/index.php/JavaScript_Performance_Best_Practices#Use_createDocumentFragment.28.29
Minimising reflows

    Apply animations with position fixed or 
    absolute
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
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
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)
jQuery Usage




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




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




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




         Source: http://www.slideshare.net/paul.irish/perfcompression
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
Sample Usage
Full Source:
1. Initialisation
2. jQuery.prototype
3. Utility methods
4. Aliases
5. Sample Plug­ins
jQuery Instance 
(Matched/Wrapped Set)
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

    1.2.6 shipping with Drupal 6

    1.4.2 shipping with Drupal 7
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
Specific on the right & generic on the left
Chain or cache selections
Don't act on empty sets
Class selectors
jQuery.fn.find
Events
Memory Leaks




  Source: http://msdn.microsoft.com/en-us/library/bb250448%28VS.85%29.aspx
Memory Leaks

    Avoid attaching objects to DOM nodes directly

    Use jQuery methods instead:
jQuery source viewer
 http://james.padolsey.com/jquery
jQuery: what's coming

  Ajax module rewrite

  Dependency & load management

  Templating

  Data binding

  Mobile support
jQuery Dublin
http://meetups.jquery.com/group/jquerydublin
Tools & Resources
Google Closure Compiler
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 browsers

  jQuery gained 13% minification improvement by 
switiching to Google Compiler from YUI compressor
dynaTrace AJAX Edition
    http://ajax.dynatrace.com/
Cuzillion
Open­source web performance exploration tool 
Books



        v
Links
Yahoo Exceptional performance team
http://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/
Thanks
http://www.slideshare.net/cistov
 http://www.twitter.com/cistov
    http://github.com/cistov/

Contenu connexe

Tendances

Tuning Web Performance
Tuning Web PerformanceTuning Web Performance
Tuning Web Performance
Eric ShangKuan
 
Pagespeed what, why, and how it works
Pagespeed   what, why, and how it worksPagespeed   what, why, and how it works
Pagespeed what, why, and how it works
Ilya Grigorik
 
Joomladay Es 2009 - Nooku Framework
Joomladay Es 2009  - Nooku FrameworkJoomladay Es 2009  - Nooku Framework
Joomladay Es 2009 - Nooku Framework
Nooku
 

Tendances (20)

Building a Next Generation Mobile Browser using Web technologies
Building a Next Generation Mobile Browser using Web technologiesBuilding a Next Generation Mobile Browser using Web technologies
Building a Next Generation Mobile Browser using Web technologies
 
Sg conference multiplatform_apps_adam_stanley
Sg conference multiplatform_apps_adam_stanleySg conference multiplatform_apps_adam_stanley
Sg conference multiplatform_apps_adam_stanley
 
Developing Series 40 web apps with Nokia Web Tools 2.0
Developing Series 40 web apps with Nokia Web Tools 2.0Developing Series 40 web apps with Nokia Web Tools 2.0
Developing Series 40 web apps with Nokia Web Tools 2.0
 
Developing web applications in 2010
Developing web applications in 2010Developing web applications in 2010
Developing web applications in 2010
 
Cordova: APIs and instruments
Cordova: APIs and instrumentsCordova: APIs and instruments
Cordova: APIs and instruments
 
Thats Not Flash?
Thats Not Flash?Thats Not Flash?
Thats Not Flash?
 
High Performance Web Sites, With Ads: Don't let third parties make you slow
High Performance Web Sites, With Ads: Don't let third parties make you slowHigh Performance Web Sites, With Ads: Don't let third parties make you slow
High Performance Web Sites, With Ads: Don't let third parties make you slow
 
How To Build a Multi-Field Search Page For Your XPages Application
How To Build a Multi-Field Search Page For Your XPages ApplicationHow To Build a Multi-Field Search Page For Your XPages Application
How To Build a Multi-Field Search Page For Your XPages Application
 
Tuning Web Performance
Tuning Web PerformanceTuning Web Performance
Tuning Web Performance
 
JMP402 Master Class: Managed beans and XPages: Your Time Is Now
JMP402 Master Class: Managed beans and XPages: Your Time Is NowJMP402 Master Class: Managed beans and XPages: Your Time Is Now
JMP402 Master Class: Managed beans and XPages: Your Time Is Now
 
HTML5 and Google Chrome - DevFest09
HTML5 and Google Chrome - DevFest09HTML5 and Google Chrome - DevFest09
HTML5 and Google Chrome - DevFest09
 
Optimizing Browser Rendering
Optimizing Browser RenderingOptimizing Browser Rendering
Optimizing Browser Rendering
 
Pagespeed what, why, and how it works
Pagespeed   what, why, and how it worksPagespeed   what, why, and how it works
Pagespeed what, why, and how it works
 
Html5 features: location, history and offline apps
Html5 features: location, history and offline appsHtml5 features: location, history and offline apps
Html5 features: location, history and offline apps
 
Booting up with polymer
Booting up with polymerBooting up with polymer
Booting up with polymer
 
Korea linuxforum2014 html5game-sangseoklim
Korea linuxforum2014 html5game-sangseoklimKorea linuxforum2014 html5game-sangseoklim
Korea linuxforum2014 html5game-sangseoklim
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Creating a Great XPages User Interface, TLCC Teamstudio Webinar - Feb, 2014
Creating a Great XPages User Interface, TLCC Teamstudio Webinar - Feb, 2014Creating a Great XPages User Interface, TLCC Teamstudio Webinar - Feb, 2014
Creating a Great XPages User Interface, TLCC Teamstudio Webinar - Feb, 2014
 
Web Components and PWA
Web Components and PWAWeb Components and PWA
Web Components and PWA
 
Joomladay Es 2009 - Nooku Framework
Joomladay Es 2009  - Nooku FrameworkJoomladay Es 2009  - Nooku Framework
Joomladay Es 2009 - Nooku Framework
 

Similaire à Front-end optimisation & jQuery Internals

Drupal Frontend Performance and Scalability
Drupal Frontend Performance and ScalabilityDrupal Frontend Performance and Scalability
Drupal Frontend Performance and Scalability
Ashok Modi
 
Csdn Drdobbs Tenni Theurer Yahoo
Csdn Drdobbs Tenni Theurer YahooCsdn Drdobbs Tenni Theurer Yahoo
Csdn Drdobbs Tenni Theurer Yahoo
guestb1b95b
 
High Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practicesHigh Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practices
Stoyan Stefanov
 
Tuning web performance
Tuning web performanceTuning web performance
Tuning web performance
George Ang
 
@media - Even Faster Web Sites
@media - Even Faster Web Sites@media - Even Faster Web Sites
@media - Even Faster Web Sites
Steve Souders
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte
 

Similaire à Front-end optimisation & jQuery Internals (20)

Open-source website performance tools
Open-source website performance toolsOpen-source website performance tools
Open-source website performance tools
 
Drupal Frontend Performance and Scalability
Drupal Frontend Performance and ScalabilityDrupal Frontend Performance and Scalability
Drupal Frontend Performance and Scalability
 
DrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingDrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizing
 
Performace optimization (increase website speed)
Performace optimization (increase website speed)Performace optimization (increase website speed)
Performace optimization (increase website speed)
 
Starting with jQuery
Starting with jQueryStarting with jQuery
Starting with jQuery
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
 
Csdn Drdobbs Tenni Theurer Yahoo
Csdn Drdobbs Tenni Theurer YahooCsdn Drdobbs Tenni Theurer Yahoo
Csdn Drdobbs Tenni Theurer Yahoo
 
Modern Web Technologies
Modern Web TechnologiesModern Web Technologies
Modern Web Technologies
 
High Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practicesHigh Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practices
 
Performance anti patterns in ajax applications
Performance anti patterns in ajax applicationsPerformance anti patterns in ajax applications
Performance anti patterns in ajax applications
 
TSSJS2010 Presenatation on: Performance Anti Patterns In Ajax Applications
TSSJS2010 Presenatation on: Performance Anti Patterns In Ajax ApplicationsTSSJS2010 Presenatation on: Performance Anti Patterns In Ajax Applications
TSSJS2010 Presenatation on: Performance Anti Patterns In Ajax Applications
 
Empowering Magnolia for Enterprise Use Cases - Experience Report
Empowering Magnolia for Enterprise Use Cases - Experience ReportEmpowering Magnolia for Enterprise Use Cases - Experience Report
Empowering Magnolia for Enterprise Use Cases - Experience Report
 
Tuning web performance
Tuning web performanceTuning web performance
Tuning web performance
 
Go With The Reflow
Go With The ReflowGo With The Reflow
Go With The Reflow
 
@media - Even Faster Web Sites
@media - Even Faster Web Sites@media - Even Faster Web Sites
@media - Even Faster Web Sites
 
Testable client side_mvc_apps_in_javascript
Testable client side_mvc_apps_in_javascriptTestable client side_mvc_apps_in_javascript
Testable client side_mvc_apps_in_javascript
 
Web components api + Vuejs
Web components api + VuejsWeb components api + Vuejs
Web components api + Vuejs
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Front-end optimisation & jQuery Internals