← Back to jQuery Course | Chapter 15: Performance & Best Practices | Lesson 1 of 5

Selectors को Cache करना

Selector caching का मतलब है एक selected jQuery object को एक variable में save करना और उसे reuse करना। यह same element को कई बार select करने से बचाता है।
Syntax
javascript
var $element = $(selector);
$element.action();

Selector Caching क्या है?

Selector caching का मतलब है एक jQuery selector को एक बार चलाना, resulting jQuery object को एक variable में store करना, और उस variable को reuse करना बजाय।

उदाहरण: What is Selector Caching?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="text">Text</p>
    <script>
      const $text = $("#text");
      $text.css("color", "red");
      $text.css("font-size", "18px");
    </script>
  </body>
</html>

Selectors क्यों Cache करें?

Caching खासतौर पर तब उपयोगी है जब वही element या elements का set एक ही function या event handler के अंदर कई बार इस्तेमाल होता है, क्योंकि यह avoid करता है।

उदाहरण: Why Cache Selectors?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      const $btn = $("#btn");
      $btn.on("click", function() {
      $btn.css("background", "yellow");
      });
    </script>
  </body>
</html>

Repeated Work से पहले Cache करें

अगर कोई loop या बार-बार trigger होने वाला event handler हर iteration या हर call पर same selector इस्तेमाल करता है, तो resulting jQuery object को एक variable में store करें।

उदाहरण: Cache Before Repeated Work

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>One</li><li>Two</li></ul>
    <script>
      const $items = $("#list li");
      $items.each(function() {
      $(this).css("color", "blue");
      });
    </script>
  </body>
</html>

Functions के अंदर Cache करें

एक cached selector अक्सर किसी function के top पर एक बार बनाया जाता है जो अपने body में same element या elements के साथ कई बार काम करता है, ताकि se।

उदाहरण: Cache Inside Functions

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Box</div>
    <script>
      function styleBox() {
      const $box = $("#box");
      $box.css("color", "red");
      $box.css("padding", "10px");
      }
      styleBox();
    </script>
  </body>
</html>

एक छोटा Caching Project

Caching एक simple, low-risk optimization है — इसे तब इस्तेमाल करें जब आप देखें कि exact same selector string किसी ऐसे code में एक से ज़्यादा बार आ रहा है जो चलता है।

उदाहरण: Small Caching Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      const $btn = $("#btn");
      $btn.on("click", function() {
      $btn.text("Clicked!");
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक loop के अंदर बार-बार $("#text") लिखना, हर iteration पर lookup चलाते हुए बजाय इसे एक बार store करने के: const $text = $("#text");।
  2. elements exist करने से पहले एक selection cache करना, ताकि variable में बाद में add किए जाने के बाद भी एक empty set रहे।
  3. एक selector को बहुत जल्दी cache करना, जैसे items add होने से पहले $(".item"), ताकि stored set बाद में बनाए गए elements शामिल न करे।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.