SlideShare a Scribd company logo
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

PPTX
Harness jQuery Templates and Data Link
PPTX
JavaScript and jQuery Basics
PPTX
React basic by Yoav Amit, Wix
PPTX
Setting up Page Object Model in Automation Framework
PPT
J Query
PDF
handout-05b
PPT
J query intro_29thsep_alok
PPTX
Tips for writing Javascript for Drupal
Harness jQuery Templates and Data Link
JavaScript and jQuery Basics
React basic by Yoav Amit, Wix
Setting up Page Object Model in Automation Framework
J Query
handout-05b
J query intro_29thsep_alok
Tips for writing Javascript for Drupal

What's hot (19)

PDF
jQuery Presentation to Rails Developers
PPTX
BOOM Performance
PDF
Merb jQuery
PPT
jQuery introduction
PPTX
Java script
PPT
HTML5 Introduction by Dhepthi L
PPTX
Angular - Beginner
KEY
Week 4 - jQuery + Ajax
PDF
Introduction to Selenium and Ruby
PDF
22 j query1
PDF
Fronted From Scratch - Supercharge Magento page speed
PDF
Associations & JavaScript
PPTX
Intro to jQuery
PPTX
jQuery Conference 2012 keynote
DOCX
Built in filters
PDF
The Onion
PPTX
jQuery - Web Engineering
PPTX
Introduction to java_script
PDF
Web Component
jQuery Presentation to Rails Developers
BOOM Performance
Merb jQuery
jQuery introduction
Java script
HTML5 Introduction by Dhepthi L
Angular - Beginner
Week 4 - jQuery + Ajax
Introduction to Selenium and Ruby
22 j query1
Fronted From Scratch - Supercharge Magento page speed
Associations & JavaScript
Intro to jQuery
jQuery Conference 2012 keynote
Built in filters
The Onion
jQuery - Web Engineering
Introduction to java_script
Web Component
Ad

Viewers also liked (9)

PPTX
Steps to happiness
PDF
Demonstrating Significant Benefit for an OMP
PPTX
PPTX
Microclase
PDF
Tech Talk: Keeping Applications Compliant and Secure Using Release Automation
PPT
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
PDF
OHMS ENERGY-ppt
PPTX
Los Comechingones
PDF
10 Sanity-checks for Efficient Data Management
Steps to happiness
Demonstrating Significant Benefit for an OMP
Microclase
Tech Talk: Keeping Applications Compliant and Secure Using Release Automation
Motivational quotes to boost your mojo - brought to you by Phi Creative Solut...
OHMS ENERGY-ppt
Los Comechingones
10 Sanity-checks for Efficient Data Management
Ad

Similar to jQuery Performance Tips and Tricks (20)

PPTX
jQuery
PPT
Jquery PPT_Finalfgfgdfgdr5tryeujkhdwappt
PPTX
How to increase Performance of Web Application using JQuery
ODP
Jquery- One slide completing all JQuery
PPT
jQuery introduction/basic concept /welcome jQuery
PPT
Testing in AngularJS
PDF
jQuery Proven Performance Tips & Tricks
PPTX
Jquery Complete Presentation along with Javascript Basics
PPTX
Jquery fundamentals
PPTX
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
PDF
Advanced JQuery Mobile tutorial with Phonegap
PPTX
lec 14-15 Jquery_All About J-query_.pptx
PPT
Don't Worry jQuery is very Easy:Learning Tips For jQuery
PPTX
PPTX
J Query The Write Less Do More Javascript Library
PPTX
SharePoint Cincy 2012 - jQuery essentials
PPTX
Introduction to jquery mobile with Phonegap
PPT
J query
jQuery
Jquery PPT_Finalfgfgdfgdr5tryeujkhdwappt
How to increase Performance of Web Application using JQuery
Jquery- One slide completing all JQuery
jQuery introduction/basic concept /welcome jQuery
Testing in AngularJS
jQuery Proven Performance Tips & Tricks
Jquery Complete Presentation along with Javascript Basics
Jquery fundamentals
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Advanced JQuery Mobile tutorial with Phonegap
lec 14-15 Jquery_All About J-query_.pptx
Don't Worry jQuery is very Easy:Learning Tips For jQuery
J Query The Write Less Do More Javascript Library
SharePoint Cincy 2012 - jQuery essentials
Introduction to jquery mobile with Phonegap
J query

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

jQuery Performance Tips and Tricks

  • 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