- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the best practices to improve jQuery selector performance?
To enhance the performance of jQuery selector, you need to perform optimization. Here are some of the techniques:
Cache Selector
Caching enhances the performance of the application. Cache your jQuery selectors for better performance. This can be done using the ID as your selector. For example, this caches the selector and stores it in variable.
var $elm = $("#elm");
Instead of using the jQuery ID selector, use the $elm variable:
var $elm = $("#elm"); $elm.addClass(‘example’);
Use ID selector
jQuery is a JavaScript library. In JavaScript, document.getElementById() is used to select the HTML element, which is the fastest way. Since jQuery is written on top of JavaScript, it calls the JavaScript functions to complete the task. Using ID as a selector in jQuery, calls the document.getElementById(), so always try to use the ID selector.
Avoid repeating selectors
Do not repeat your selector and use chaining, to chain multiple methods in a single call. Let’s say the following is the code:
Here’s the optimized version:
$("div").css({"color", "blue", "font-size", "12px"}).text("This is demo text.”);
- Related Articles
- What are the Best Practices to Improve CRM User Adoption?
- What are the best practices to organize Python modules?
- What are the Best Practices in Visual Marketing?
- What are Python coding standards/best practices?
- What are the best practices for committing in Git?
- What are the best practices to be followed while using JavaScript?
- How to improve the performance of JavaScript?
- What are the best practices for using loops in Python?
- What are the best practices for exception handling in Python?
- What are the best practices for function overloading in JavaScript?
- What are favicon best practices regarding size and format?
- What are the best practices for using if statements in Python?
- What is the difference between jQuery(selector) and $(selector)?
- What are the “best practices” for using import in a Python module?
- What are the best practices to keep in mind while using packages in Java?
