SlideShare a Scribd company logo
1 of 14
jQuery Performance
Tips and Tricks
About Me
Front-end Developer at Booking.com
Previously worked as a Full-stack Developer at Art. Lebedev Studio
Technical writer, own blog https://www.codingbox.io/
Some other projects
I’m still using jQuery
Why:
It’s very quick and easy to start with jQuery
Allows to handle variety of browsers with ease
Where:
At work
For hobby-projects (e. g. I created oldams.nl using jQuery)
Why performance?
Better user experience (also, customers rarely complain about performance,
they just leave)
Google takes performance into account for PageRank
Basic tip. Stay up to date.
Always use the latest jQuery version, newer versions of jQuery contain
performance improvements and security updates, thus benefit of upgrade
is instant
Make sure you’re not harming customers with upgrade - use jQuery Migrate
Make sure site will work with injected older version of jQuery (don’t rely on
globals $, jQuery)
Selectors
Selectors performance vary much
Fastest to slowest:
ID selectors: $(‘#awesome-element’)
Element selectors: $(‘form’)
Class selectors: $(‘.awesome-element’)
Pseudo & attribute selectors: $(‘[data-attr]’), $(‘:hidden’)
ID and element selectors are backed by Native DOM operations
jQuery uses right-to-left selectors engine
Same rule for querySelectorAll
Cases:
$(‘div.page div.block .element’)
$(‘div.page .element’)
$(‘.page div.element’) best
$(‘#container’).find(‘.element’) way faster than $(‘#container .element’)
You can use context: $(‘.element’, ‘#container’)
Always cache selectors when possible
BAD
var block = $(‘.block’);
var elements = $(‘.block’).find(‘.element’);
var title = $(‘.block’).data(‘title’);
GOOD
var block = $(‘.block’);
var elements = block.find(‘.element’);
var title = block.data(‘title’);
Avoid DOM touches as much as possible
Example:
$(‘<style type=”text/css”>.awesome-class { color: red;
}</style>’).appendTo(‘head’);
is taking ~ constant time in every case and might be faster than
$(‘.awesome-class’).css(‘color’, ‘red’);
Avoid DOM touches as much as possible
Prefer building HTML strings and use append() as late as possible
For heavy operations on existing DOM, use detach() and put it back later
Prefer .data() over .attr() to work with data associated with element (.data()
allows you to avoid storing data in DOM)
Prefer group queries over loops
BAD
$(‘.element’).each(function() {
$(this).something().somethingElse();
});
GOOD
$(‘.element’).something().somethingElse();
Check if element exists before using it
BAD
$(‘.element’).slideDown();
GOOD
if ($(‘.element’).length) {
$(‘.element’).slideDown(); // this is a heavy call
}
Read jQuery source when in doubt
https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js
https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.js
https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.js
Thank you!
Follow me:
twitter.com/@viatsko
medium.com/@viatsko
codingbox.io

More Related Content

What's hot

jQuery Presentation to Rails Developers
jQuery Presentation to Rails DevelopersjQuery Presentation to Rails Developers
jQuery Presentation to Rails DevelopersYehuda Katz
 
BOOM Performance
BOOM PerformanceBOOM Performance
BOOM Performancedapulse
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introductionTomi Juhola
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajaxbaygross
 
Introduction to Selenium and Ruby
Introduction to Selenium and RubyIntroduction to Selenium and Ruby
Introduction to Selenium and RubyYnon Perek
 
Fronted From Scratch - Supercharge Magento page speed
Fronted From Scratch - Supercharge Magento page speedFronted From Scratch - Supercharge Magento page speed
Fronted From Scratch - Supercharge Magento page speedYousef Cisco
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScriptJoost Elfering
 
jQuery Conference 2012 keynote
jQuery Conference 2012 keynotejQuery Conference 2012 keynote
jQuery Conference 2012 keynotedmethvin
 
jQuery - Web Engineering
jQuery - Web Engineering jQuery - Web Engineering
jQuery - Web Engineering adeel990
 

What's hot (19)

jQuery Presentation to Rails Developers
jQuery Presentation to Rails DevelopersjQuery Presentation to Rails Developers
jQuery Presentation to Rails Developers
 
BOOM Performance
BOOM PerformanceBOOM Performance
BOOM Performance
 
Merb jQuery
Merb jQueryMerb jQuery
Merb jQuery
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Java script
Java scriptJava script
Java script
 
HTML5 Introduction by Dhepthi L
HTML5 Introduction by Dhepthi LHTML5 Introduction by Dhepthi L
HTML5 Introduction by Dhepthi L
 
Angular - Beginner
Angular - BeginnerAngular - Beginner
Angular - Beginner
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
Introduction to Selenium and Ruby
Introduction to Selenium and RubyIntroduction to Selenium and Ruby
Introduction to Selenium and Ruby
 
22 j query1
22 j query122 j query1
22 j query1
 
Fronted From Scratch - Supercharge Magento page speed
Fronted From Scratch - Supercharge Magento page speedFronted From Scratch - Supercharge Magento page speed
Fronted From Scratch - Supercharge Magento page speed
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScript
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
jQuery Conference 2012 keynote
jQuery Conference 2012 keynotejQuery Conference 2012 keynote
jQuery Conference 2012 keynote
 
Built in filters
Built in filtersBuilt in filters
Built in filters
 
The Onion
The OnionThe Onion
The Onion
 
jQuery - Web Engineering
jQuery - Web Engineering jQuery - Web Engineering
jQuery - Web Engineering
 
Introduction to java_script
Introduction to java_scriptIntroduction to java_script
Introduction to java_script
 
Web Component
Web ComponentWeb Component
Web Component
 

Viewers also liked

Steps to happiness
Steps to happinessSteps to happiness
Steps to happinessMykB101
 
Demonstrating Significant Benefit for an OMP
Demonstrating Significant Benefit for an OMPDemonstrating Significant Benefit for an OMP
Demonstrating Significant Benefit for an OMPMauro Placchi
 
Tech Talk: Keeping Applications Compliant and Secure Using Release Automation
Tech Talk: Keeping Applications Compliant and Secure Using Release AutomationTech Talk: Keeping Applications Compliant and Secure Using Release Automation
Tech Talk: Keeping Applications Compliant and Secure Using Release AutomationCA Technologies
 
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...Phi Creative Solutions Pvt. Ltd.
 
10 Sanity-checks for Efficient Data Management
10 Sanity-checks for Efficient Data Management 10 Sanity-checks for Efficient Data Management
10 Sanity-checks for Efficient Data Management CRMT Digital
 

Viewers also liked (9)

Steps to happiness
Steps to happinessSteps to happiness
Steps to happiness
 
Demonstrating Significant Benefit for an OMP
Demonstrating Significant Benefit for an OMPDemonstrating Significant Benefit for an OMP
Demonstrating Significant Benefit for an OMP
 
Tugas
TugasTugas
Tugas
 
Microclase
MicroclaseMicroclase
Microclase
 
Tech Talk: Keeping Applications Compliant and Secure Using Release Automation
Tech Talk: Keeping Applications Compliant and Secure Using Release AutomationTech Talk: Keeping Applications Compliant and Secure Using Release Automation
Tech Talk: Keeping Applications Compliant and Secure Using Release Automation
 
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
 
OHMS ENERGY-ppt
OHMS ENERGY-pptOHMS ENERGY-ppt
OHMS ENERGY-ppt
 
Los Comechingones
Los ComechingonesLos Comechingones
Los Comechingones
 
10 Sanity-checks for Efficient Data Management
10 Sanity-checks for Efficient Data Management 10 Sanity-checks for Efficient Data Management
10 Sanity-checks for Efficient Data Management
 

Similar to Optimize jQuery Performance with Selectors, Caching and Avoiding DOM Touches

How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuerykolkatageeks
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQueryKnoldus Inc.
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJSPeter Drinnan
 
jQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksjQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksAddy Osmani
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Roy de Kleijn
 
Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Rakesh Jha
 
Don't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQueryDon't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQueryshabab shihan
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Libraryrsnarayanan
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsMark Rackley
 
Introduction to jquery mobile with Phonegap
Introduction to jquery mobile with PhonegapIntroduction to jquery mobile with Phonegap
Introduction to jquery mobile with PhonegapRakesh Jha
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And TricksLester Lievens
 

Similar to Optimize jQuery Performance with Selectors, Caching and Avoiding DOM Touches (20)

jQuery
jQueryjQuery
jQuery
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuery
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
jQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksjQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & Tricks
 
JQuery
JQueryJQuery
JQuery
 
JQuery
JQueryJQuery
JQuery
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
 
Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap
 
Don't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQueryDon't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQuery
 
jQuery
jQueryjQuery
jQuery
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
 
Introduction to jquery mobile with Phonegap
Introduction to jquery mobile with PhonegapIntroduction to jquery mobile with Phonegap
Introduction to jquery mobile with Phonegap
 
J query
J queryJ query
J query
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
J query1
J query1J query1
J query1
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And Tricks
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

Optimize jQuery Performance with Selectors, Caching and Avoiding DOM Touches

  • 2. About Me Front-end Developer at Booking.com Previously worked as a Full-stack Developer at Art. Lebedev Studio Technical writer, own blog https://www.codingbox.io/ Some other projects
  • 3. I’m still using jQuery Why: It’s very quick and easy to start with jQuery Allows to handle variety of browsers with ease Where: At work For hobby-projects (e. g. I created oldams.nl using jQuery)
  • 4. Why performance? Better user experience (also, customers rarely complain about performance, they just leave) Google takes performance into account for PageRank
  • 5. Basic tip. Stay up to date. Always use the latest jQuery version, newer versions of jQuery contain performance improvements and security updates, thus benefit of upgrade is instant Make sure you’re not harming customers with upgrade - use jQuery Migrate Make sure site will work with injected older version of jQuery (don’t rely on globals $, jQuery)
  • 6. Selectors Selectors performance vary much Fastest to slowest: ID selectors: $(‘#awesome-element’) Element selectors: $(‘form’) Class selectors: $(‘.awesome-element’) Pseudo & attribute selectors: $(‘[data-attr]’), $(‘:hidden’) ID and element selectors are backed by Native DOM operations
  • 7. jQuery uses right-to-left selectors engine Same rule for querySelectorAll Cases: $(‘div.page div.block .element’) $(‘div.page .element’) $(‘.page div.element’) best $(‘#container’).find(‘.element’) way faster than $(‘#container .element’) You can use context: $(‘.element’, ‘#container’)
  • 8. Always cache selectors when possible BAD var block = $(‘.block’); var elements = $(‘.block’).find(‘.element’); var title = $(‘.block’).data(‘title’); GOOD var block = $(‘.block’); var elements = block.find(‘.element’); var title = block.data(‘title’);
  • 9. Avoid DOM touches as much as possible Example: $(‘<style type=”text/css”>.awesome-class { color: red; }</style>’).appendTo(‘head’); is taking ~ constant time in every case and might be faster than $(‘.awesome-class’).css(‘color’, ‘red’);
  • 10. Avoid DOM touches as much as possible Prefer building HTML strings and use append() as late as possible For heavy operations on existing DOM, use detach() and put it back later Prefer .data() over .attr() to work with data associated with element (.data() allows you to avoid storing data in DOM)
  • 11. Prefer group queries over loops BAD $(‘.element’).each(function() { $(this).something().somethingElse(); }); GOOD $(‘.element’).something().somethingElse();
  • 12. Check if element exists before using it BAD $(‘.element’).slideDown(); GOOD if ($(‘.element’).length) { $(‘.element’).slideDown(); // this is a heavy call }
  • 13. Read jQuery source when in doubt https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.js https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.js