SlideShare a Scribd company logo
jQuery Doing it right Girish  Gangadharan @appoosa girish@giri.sh http://giri.sh
Tools for the trade Proper tools = Enhanced productivity + Makes work fun ,[object Object]
 Chrome Developer Tools
 IE Web Developer Toolbar
 Fiddler,[object Object]
 Only 4KB minified and gzipped
 Highly extensible with easy-to-use API,[object Object]
 Use tag names in selectors
 Be more specific on the right side query
 Don’t overdo the selectors,[object Object]
Cache is awesome Use it as often and as much as you can Limited DOM traversal = Better performance var $h2s = $('#container h2');         	$h2s.css('border', '1px solid #666666'); 	$h2s.click( function() {             $(this).next('div').slideToggle();         });
'Context' is fast. 'Find' is faster. Try not to involve the Sizzle engine if you can             $('child', $('#parent'));            (internally calls)           $('#parent').find('child');
Chaining Less code. Better readability. $('p#elementId') .css('border', '3px dashed yellow') .css('background-color', 'orange') .fadeIn('slow');
Bind() Vs Live() Vs Delegate() Understand what each does. Use appropriately. $('#sometable').each(function(){         $('td', this).bind('hover', function(){         	$(this).toggleClass('hover');         });  }); $('#sometable').each(function(){         $('td', this).live('hover', function(){         	$(this).toggleClass('hover');         });  }); $('#sometable').delegate('td', 'hover', function(){         $(this).toggleClass('hover'); });
Native calls Vs jQuery Native calls are always faster even if a tad bit For example, $(this).attr('id'); // Fast this.id; // Faster
Limit DOM manipulation Create what you need in memory. Then update the DOM.  BAD var $mylist = $('#mylist'); // <ul>  for (vari = 0;  i < 100;  i++) {       $mylist.append('<li>' + bestsellers[i] + '</li>');  } GOOD var $mylist = $('#mylist'); // <ul>  varconcatenatedList = “”; for (vari = 0;  i < 100;  i++) { concatenatedList  += ('<li>' + bestsellers[i] + '</li>');  } $mylist.append(concatenatedList);
Event delegation Bind fewer events to the elements for better performance. BAD $('#myListli).bind('click', function() {                 // do stuff  }); GOOD $('#myList).bind('click', function(e){  var target = e.target; if (target.nodeName === 'LI') {  // do stuff }  });

More Related Content

What's hot

Twiggy - let's get our widget on!
Twiggy - let's get our widget on!Twiggy - let's get our widget on!
Twiggy - let's get our widget on!
Elliott Kember
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
mohd rozani abd ghani
 

What's hot (20)

PHP and CSS
PHP and CSSPHP and CSS
PHP and CSS
 
Google drive on linux
Google drive on linuxGoogle drive on linux
Google drive on linux
 
Twiggy - let's get our widget on!
Twiggy - let's get our widget on!Twiggy - let's get our widget on!
Twiggy - let's get our widget on!
 
Simple Usability Tweaks for Your WordPress Theme
Simple Usability Tweaks for Your WordPress ThemeSimple Usability Tweaks for Your WordPress Theme
Simple Usability Tweaks for Your WordPress Theme
 
Creating Themes
Creating ThemesCreating Themes
Creating Themes
 
Client-side Transformations
Client-side TransformationsClient-side Transformations
Client-side Transformations
 
Own Your Front-end Performance: Tools, Not Rules
Own Your Front-end Performance: Tools, Not RulesOwn Your Front-end Performance: Tools, Not Rules
Own Your Front-end Performance: Tools, Not Rules
 
Own Your Front-end Performance: Tools, Not Rules
Own Your Front-end Performance: Tools, Not RulesOwn Your Front-end Performance: Tools, Not Rules
Own Your Front-end Performance: Tools, Not Rules
 
The Why Of Ruby
The Why Of RubyThe Why Of Ruby
The Why Of Ruby
 
RSpec. Part 1
RSpec. Part 1RSpec. Part 1
RSpec. Part 1
 
Better Selenium Tests with Geb - Selenium Conf 2014
Better Selenium Tests with Geb - Selenium Conf 2014Better Selenium Tests with Geb - Selenium Conf 2014
Better Selenium Tests with Geb - Selenium Conf 2014
 
oik-plugins: WordUp Pompey! Nov 2011
oik-plugins: WordUp Pompey! Nov 2011oik-plugins: WordUp Pompey! Nov 2011
oik-plugins: WordUp Pompey! Nov 2011
 
Performance
PerformancePerformance
Performance
 
Conditional Love - Using WordPress Conditional Tags to Write More Effective T...
Conditional Love - Using WordPress Conditional Tags to Write More Effective T...Conditional Love - Using WordPress Conditional Tags to Write More Effective T...
Conditional Love - Using WordPress Conditional Tags to Write More Effective T...
 
Word Camp Fukuoka2010
Word Camp Fukuoka2010Word Camp Fukuoka2010
Word Camp Fukuoka2010
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
 
Behat for writing tests in a stylized way
Behat for writing tests in a stylized wayBehat for writing tests in a stylized way
Behat for writing tests in a stylized way
 
Create An Amazing Apps For The Google Assistant!
Create An Amazing Apps For The Google Assistant!Create An Amazing Apps For The Google Assistant!
Create An Amazing Apps For The Google Assistant!
 
Red beanphp orm presentation
Red beanphp orm presentationRed beanphp orm presentation
Red beanphp orm presentation
 
Zen based theming
Zen based themingZen based theming
Zen based theming
 

Viewers also liked

Audit Of The Charlie Ticketing System
Audit Of The Charlie Ticketing SystemAudit Of The Charlie Ticketing System
Audit Of The Charlie Ticketing System
team china
 

Viewers also liked (7)

Crystals
CrystalsCrystals
Crystals
 
Audit Of The Charlie Ticketing System
Audit Of The Charlie Ticketing SystemAudit Of The Charlie Ticketing System
Audit Of The Charlie Ticketing System
 
Efektywnie i efektownie
Efektywnie i efektownieEfektywnie i efektownie
Efektywnie i efektownie
 
jQuery Tips Tricks Trivia
jQuery Tips Tricks TriviajQuery Tips Tricks Trivia
jQuery Tips Tricks Trivia
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similar to jQuery - Doing it right

jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish82
 
Zendcon 2007 Features
Zendcon 2007 FeaturesZendcon 2007 Features
Zendcon 2007 Features
fivespeed5
 
Awash in a sea of connections
Awash in a sea of connectionsAwash in a sea of connections
Awash in a sea of connections
Galen Charlton
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
guest5d87aa6
 

Similar to jQuery - Doing it right (20)

Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
jQuery Performance Rules
jQuery Performance RulesjQuery Performance Rules
jQuery Performance Rules
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
 
Getting started with MongoDB and PHP
Getting started with MongoDB and PHPGetting started with MongoDB and PHP
Getting started with MongoDB and PHP
 
Zendcon 2007 Features
Zendcon 2007 FeaturesZendcon 2007 Features
Zendcon 2007 Features
 
Awash in a sea of connections
Awash in a sea of connectionsAwash in a sea of connections
Awash in a sea of connections
 
SlideShare Instant
SlideShare InstantSlideShare Instant
SlideShare Instant
 
SlideShare Instant
SlideShare InstantSlideShare Instant
SlideShare Instant
 
HTML::FormHandler
HTML::FormHandlerHTML::FormHandler
HTML::FormHandler
 
Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Jquery
JqueryJquery
Jquery
 
JavaScript Needn't Hurt!
JavaScript Needn't Hurt!JavaScript Needn't Hurt!
JavaScript Needn't Hurt!
 
Web Scraping with PHP
Web Scraping with PHPWeb Scraping with PHP
Web Scraping with PHP
 
Jquery optimization-tips
Jquery optimization-tipsJquery optimization-tips
Jquery optimization-tips
 
Faking Data
Faking DataFaking Data
Faking Data
 
Key Value Storage Systems ... and Beyond ... with Python
Key Value Storage Systems ... and Beyond ... with PythonKey Value Storage Systems ... and Beyond ... with Python
Key Value Storage Systems ... and Beyond ... with Python
 

More from girish82 (7)

Introduction to node
Introduction to nodeIntroduction to node
Introduction to node
 
Designing better-ux
Designing better-uxDesigning better-ux
Designing better-ux
 
Designing better-ux-workshop-2
Designing better-ux-workshop-2Designing better-ux-workshop-2
Designing better-ux-workshop-2
 
Designing better-ux-workshop-3
Designing better-ux-workshop-3Designing better-ux-workshop-3
Designing better-ux-workshop-3
 
Designing better-ux-workshop-4
Designing better-ux-workshop-4Designing better-ux-workshop-4
Designing better-ux-workshop-4
 
Designing better-ux-workshop-5
Designing better-ux-workshop-5Designing better-ux-workshop-5
Designing better-ux-workshop-5
 
Why we need to hire UX professionals
Why we need to hire UX professionalsWhy we need to hire UX professionals
Why we need to hire UX professionals
 

Recently uploaded

Recently uploaded (20)

IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 

jQuery - Doing it right

  • 1. jQuery Doing it right Girish Gangadharan @appoosa girish@giri.sh http://giri.sh
  • 2.
  • 4. IE Web Developer Toolbar
  • 5.
  • 6. Only 4KB minified and gzipped
  • 7.
  • 8. Use tag names in selectors
  • 9. Be more specific on the right side query
  • 10.
  • 11. Cache is awesome Use it as often and as much as you can Limited DOM traversal = Better performance var $h2s = $('#container h2'); $h2s.css('border', '1px solid #666666'); $h2s.click( function() { $(this).next('div').slideToggle(); });
  • 12. 'Context' is fast. 'Find' is faster. Try not to involve the Sizzle engine if you can $('child', $('#parent')); (internally calls) $('#parent').find('child');
  • 13. Chaining Less code. Better readability. $('p#elementId') .css('border', '3px dashed yellow') .css('background-color', 'orange') .fadeIn('slow');
  • 14. Bind() Vs Live() Vs Delegate() Understand what each does. Use appropriately. $('#sometable').each(function(){ $('td', this).bind('hover', function(){ $(this).toggleClass('hover'); }); }); $('#sometable').each(function(){ $('td', this).live('hover', function(){ $(this).toggleClass('hover'); }); }); $('#sometable').delegate('td', 'hover', function(){ $(this).toggleClass('hover'); });
  • 15. Native calls Vs jQuery Native calls are always faster even if a tad bit For example, $(this).attr('id'); // Fast this.id; // Faster
  • 16. Limit DOM manipulation Create what you need in memory. Then update the DOM. BAD var $mylist = $('#mylist'); // <ul> for (vari = 0; i < 100; i++) { $mylist.append('<li>' + bestsellers[i] + '</li>'); } GOOD var $mylist = $('#mylist'); // <ul> varconcatenatedList = “”; for (vari = 0; i < 100; i++) { concatenatedList += ('<li>' + bestsellers[i] + '</li>'); } $mylist.append(concatenatedList);
  • 17. Event delegation Bind fewer events to the elements for better performance. BAD $('#myListli).bind('click', function() { // do stuff }); GOOD $('#myList).bind('click', function(e){ var target = e.target; if (target.nodeName === 'LI') { // do stuff } });
  • 18.
  • 19. Use data() to store state
  • 21.
  • 26. etc.Fantastic book on JS patterns: http://addyosmani.com/blog/essentialjsdesignpatternsupdate1
  • 27. Let’s play with Firebug a little.
  • 29. jQuery Doing it right. Thank you. Girish Gangadharan @appoosa girish@giri.sh http://giri.sh

Editor's Notes

  1. Position selectors :first, :last, :even, :odd, :gt, :lt, :eqEasy Form selectors :input, :text, :checkbox, :file, :password, :submit, :image, :reset, :buttonFor example, querySelectorAll doesn’t work in IE7 or lower. Sizzle kicks in then.
  2. Users will often start using the page before it finishes loading. Google Analytics and other scripts can cause the DOMContentLoaded event fire long after the page appears to be loaded.
  3. var $myBox = $(&apos;#test&apos;);  alert($myBox.html()); //This works.